LazyTS is a PowerShell script to manage Sessions and Processes on local or remote machines. It allows you to Query/Disconnect/Stop session(s), Query/Stop process(es) andSend Interactive message to one or more sessions.

This tool is using the module PSTerminalService which relies on the Cassia .NET Library.I used Sapien PowerShell Studio 2014to which make life easier if you want to start building WinForms tools and add PowerShell code.

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 asQwinsta/Rwinsta,Query/Reset SessionorQuser.

Legacy tools: Qwinsta/Query Session/Quser

PowerShell Way

PSTerminalServices Module

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 Cassiawhich 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.

PSTerminalServices cmdlets:

Disconnect-TSSession - Disconnects any attached user from the session.

- Disconnects any attached user from the session. Get-TSCurrentSession - Provides information about the session in which the current process is running.

- Provides information about the session in which the current process is running. Get-TSServers - Enumerates all terminal servers in a given domain.

- Enumerates all terminal servers in a given domain. Get-TSProcess - Gets a list of processes running in a specific session or in all sessions.

- Gets a list of processes running in a specific session or in all sessions. Get-TSSession - Lists the sessions on a given terminal server.

- Lists the sessions on a given terminal server. Send-TSMessage - Displays a message box in the specified session ID.

- Displays a message box in the specified session ID. Stop-TSProcess - Terminates the process running in a specific session or in all sessions.

- 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.

LazyTS

LazyTSis a PowerShell script to manage Terminal Services Sessions and Processes on local or remote machines. It also allows you to Disconnect/Stop sessions and Send Interactive message to one or more sessions.

Version

1.0 Initial version

Features

Session Management: Query/Disconnect

Stop

Processes Management: Query/Stop

Message : Send message to Session(s)

Requirements

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:

Download and Details