Skip to content

Instantly share code, notes, and snippets.

@daveselias
Created March 16, 2014 15:57
Show Gist options
  • Save daveselias/9585309 to your computer and use it in GitHub Desktop.
Save daveselias/9585309 to your computer and use it in GitHub Desktop.
Function Kill-Sessions{
Param(
[Array]$Computer
)
$AllSessions=@()
[int]$Number = 0
ForEach($ComputerName in $Computer){
$SystemSessions = (Get-LoggedOnUser -ComputerName $ComputerName -ErrorAction SilentlyContinue)
ForEach($Session in $SystemSessions){
$Number=($Number+1)
$Session | Add-Member -MemberType NoteProperty -Name "Number" -Value $Number
$AllSessions += $Session
}
}
$AllSessions = $AllSessions | Where-Object {($_.IdleTime -like "*:*") -and ($_.IdleTime -gt "00:59")}
$AllSessions | Select Number, `|, ComputerName, UserName, State, IdleTime, SessionName | ft -AutoSize
$Terminate=@()
$Terminate = Read-Host "Would you like to end one of the above sessions?"
IF($Terminate -like "Y*"){
$End = Read-Host "Enter the Number of the session you would like to end"
$KillSession = ($Allsessions | ? {$_.number -eq $End})
$Serv = ($Killsession).ServerName.ToString()
$ID = ($KillSession).SessionID.tostring()
Invoke-Expression "& `"LOGOFF`" /SERVER:$Serv $ID /V"
}
}
$AllServers = (Get-ADComputer -filter {name -like "*Servername*"}).name
Kill-Sessions -Computer $AllServers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment