Once in a while I need to disconnect or close one or more Terminal Sessions on a Windows Server in order to be able to connect via Remote Desktop Protocol or free up a session for someone else. You would most likely use handy legacy tools such as Qwinsta/Rwinsta, Query/Reset Session or Quser.
|Legacy tools: Qwinsta/Query Session/Quser|
While those legacy tools are great, I was looking for a PowerShell way to deal with what the command line tools had to offer and there was nothing around. Finally I found something very interesting called “PSTerminalServices”, a module created by the PowerShell MVP Shay Levy.
This module is based on a .NET library called Cassia which focus on Terminal Session (Remote Desktop) Management Tasks. So basically Shay created some wrappers around it in the form of advanced functions.
Here is a list of the main feature of this module:
Terminal Session Management Tasks:
- Enumerating terminal sessions and reporting session information including connection state, user name, client name, client display details, client-reported IP address, and client build number.
- Logging off a session
- Disconnecting a session.
- Displaying a message box in a session.
- Enumerating all processes or processes for a specified session.
- Killing a process.
- Disconnect-TSSession - Disconnects any attached user from the session.
- Get-TSCurrentSession - Provides information about the session in which the current process is running.
- Get-TSServers - Enumerates all terminal servers in a given domain.
- Get-TSProcess - Gets a list of processes running in a specific session or in all sessions.
- Get-TSSession - Lists the sessions on a given terminal server.
- Send-TSMessage - Displays a message box in the specified session ID.
- Stop-TSProcess - Terminates the process running in a specific session or in all sessions.
- Stop-TSSession - Logs the session off, disconnecting any user that might be connected.
- 1.0 Initial version
- Session Management: Query/Disconnect
- Processes Management: Query/Stop
- Message : Send message to Session(s)
- PowerShell 3.0
- Cassia Library (included in the download)
LazyTS is relying on the Cassia .NET Library (A DLL file). This file need to be present in the same directory as the script (Either when you use the PS1 or EXE file)
Of course you also need the appropriate permission on the remote machine to be able to manage it.
If the Cassia.dll is not present, the script won’t start: