Skip to content

Instantly share code, notes, and snippets.

@rossnz
Last active June 8, 2016 09:35
Show Gist options
  • Save rossnz/72af1c5d2ced025b8ffa264bf0f10cac to your computer and use it in GitHub Desktop.
Save rossnz/72af1c5d2ced025b8ffa264bf0f10cac to your computer and use it in GitHub Desktop.
Log yourself off from old TS / RDP sessions. Remote computer name can be passed as a parameter, or on the pipeline. Useful if you have to log out of a lot of machines :)
function Exit-TsSession {
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,
ValueFromPipeline=$True)]
[string[]]$computername
)
begin {
$username = $env:USERNAME
}
process {
Write-Verbose "Computername: $computername"
Write-Verbose "Username: $username"
$session = ((quser /server:$computername | ? { $_ -match $username }) -split ' +')[2]
Write-Verbose "Session: $session"
Write-Verbose "Logging $username off from $computername..."
logoff $session /server:$computername
Write-Verbose "Done!"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment