Two helpful PowerShell functions to help you check who is logged on remotely and to remotely log them off.
Remotely Get a Logged On User
This Get-LoggedOnUser will use Get-WmiObject to tell you who is logged on to a remote computer. You may use a comma-delimited list of computer names.
Function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function Get-LoggedOnUser { [CmdletBinding()] param ( [Parameter()] [ValidateScript({ Test-Connection -ComputerName $_ -Quiet -Count 1 })] [ValidateNotNullOrEmpty()] [string[]]$ComputerName = $env:COMPUTERNAME ) foreach ($comp in $ComputerName) { $output = @{ 'ComputerName' = $comp } $output.UserName = (Get-WmiObject -Class win32_computersystem -ComputerName $comp).UserName [PSCustomObject]$output } } |
Example:
1 | Get-LoggedOnUser -ComputerName "SERVER01", "SERVER02", "SERVER03" |
Remotely Logoff a Logged On User
This LogOff-LoggedOnUser function will use Get-WmiObject to log off a user. You may use a comma-delimited list of computer names.
Function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function LogOff-LoggedOnUser { [CmdletBinding()] param ( [Parameter()] [ValidateScript({ Test-Connection -ComputerName $_ -Quiet -Count 1 })] [ValidateNotNullOrEmpty()] [string[]]$ComputerName = $env:COMPUTERNAME ) foreach ($comp in $ComputerName) { $output = @{ 'ComputerName' = $comp } $output.UserName = (Get-WmiObject -Class win32_operatingsystem -ComputerName $comp).Win32Shutdown(4) [PSCustomObject]$output } } |
Example:
1 | LogOff-LoggedOnUser -ComputerName "SERVER01", "SERVER02", "SERVER03" |