Remote Desktop Services R2 Resource Kit

November 23, 2010


My new friends Christa Anderson and Kristin L. Griffin wrote the Microsoft Windows Server 2008 R2 Remote Desktop Services Resource Kit for which they asked me to contribute a “From the Field” article that examines the PowerShell TS Module I created. 

This is the code example I wrote for them. The script gets all RD Session host servers in a domain, finds all active sessions that have been running for over an hour and disconnects them in order to save server resources:


1. Shows the usage of the –WhatIf parameter, what will happen if I execute the command.

"Server1","Server2" | Foreach-Object{  
Get-TSSession -ComputerName $_ -Filter {$_.IdleTime -gt '01:00:00'}
} | Disconnect-TSSession -WhatIf

What if: Performing operation "Disconnecting session id '0'" on Target "Server1".
What if: Performing operation "Disconnecting session id '2'" on Target "Server2".

2. The first command gets all RD servers from the current domain and disconnects sessions that have been idle for more than an hour. By default, Disconnect-TSSession prompts for confirmation before it disconnects the sessions. To suppress confirmations add the –Force or $Confirm:$false parameter.

Get-TSServers | Get-TSSession -Filter {$_.IdleTime -gt '01:00:00' } | `
Disconnect-TSSession -Force

- or -

Get-TSServers | Get-TSSession -Filter {$_.IdleTime -gt '01:00:00' } | `
Disconnect-TSSession -Confirm:$false
Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>