forked from mtikoian/SQLDBA-SSMS-Solution
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLog-Off-Users-with-PowerShell.sql
36 lines (33 loc) · 1.1 KB
/
Log-Off-Users-with-PowerShell.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# How To Log Off Windows Users Remotely With PowerShell
$Servers = @('YourServerName')
$scriptBlock = {
$ErrorActionPreference = 'Stop'
try {
## Find all sessions matching the specified username
$sessions = quser | Where-Object {$_ -match 'adwivedi'}
## Parse the session IDs from the output
$sessionIds = ($sessions -split ' +')[2]
Write-Host "Found $(@($sessionIds).Count) user login(s) on computer."
## Loop through each session ID and pass each to the logoff command
$sessionIds | ForEach-Object {
Write-Host "Logging off session id [$($_)]..."
logoff $_
}
} catch {
if ($_.Exception.Message -match 'No user exists') {
Write-Host "The user is not logged in."
} else {
throw $_.Exception.Message
}
}
}
foreach($srv in $Servers)
{
Write-Host "checking $srv.." -ForegroundColor Yellow;
try {
Invoke-Command -ComputerName $srv -ScriptBlock $scriptBlock -ErrorAction Continue
}
catch {
Write-Host "Error $srv.." -ForegroundColor Red;
}
}