qwinsta /server:somesrv equivalent in Powershell? qwinsta /server:somesrv equivalent in Powershell? powershell powershell

qwinsta /server:somesrv equivalent in Powershell?


There are multiple alternatives:

  • Use the Terminal Services PowerShell Module. Easy solution.
  • Writing a powershell wrapper that parses the output of qwinsta to objects. Easy solution. See example below
  • Use the Cassia.DLL .Net wrapper to access the native APIs that qwinsta runs behind the scene. This is the class that the TS Module uses. More difficult, but will have the benefit of being customized to your needs.
  • Go crazy and use the Native Methods that Cassia.DLL accesses using P/Invoke (wtsapi32.dll, kernel32.dll, winsta.dll). Hard and overcomplicated.

PowerShell-wrapper for qwinsta

function Get-TSSessions {    param(        $ComputerName = "localhost"    )    qwinsta /server:$ComputerName |    #Parse output    ForEach-Object {        $_.Trim() -replace "\s+",","    } |    #Convert to objects    ConvertFrom-Csv}Get-TSSessions -ComputerName "localhost" | ft -AutoSizeSESSIONNAME USERNAME ID     STATE  TYPE DEVICE----------- -------- --     -----  ---- ------services    0        Disc                     console     Frode    1      Active            rdp-tcp     65537    Listen     #This is objects, so we can manipulate the results to get the info we want. Active sessions only:Get-TSSessions -ComputerName "localhost" | ? { $_.State -eq 'Active' } | ft -AutoSize SessionName, UserName, IDSESSIONNAME USERNAME ID----------- -------- --console     Frode    1           


I used to use Terminal Services PowerShell Module (now in codeplex archive), but it was two years ago. I can't put my hand on it, but it also exists a function on gitshub or another site that embeded QWinsta/RmWinsta.