top of page

PowerShell Script to Log Off Remote Users

Updated: Jul 18

Article No :: 30


PowerShell Script to Log Off Remote Users
PowerShell Script to Log Off Remote Users

🔐PowerShell Script to Log Off Remote Users After Validation from Text Files


🧭 Introduction

In enterprise environments, IT administrators often need to log off specific users from remote machines, especially for security, compliance, or maintenance reasons. But what if you need to do this in bulk, and also log which users weren’t found? Our PowerShell Script to Log Off Remote Users After Validation from Text Files is very powerful and tested.


This PowerShell-based solution enables you to:


✅ Check whether a remote computer is online

✅ Verify if PowerShell remoting (WinRM) is enabled

✅ Confirm if the specified user account exists

✅ Log off that user if found, or log a message if not


Everything is controlled via two simple input files:

  • User.txt → List of usernames

  • computers.txt → List of remote computer names


⚙️ Prerequisites


Before running the script, ensure the following:

🔹 PowerShell 5.1 or later is installed

🔹 Administrator privileges to execute the script

🔹 WinRM (Windows Remote Management) is enabled on all target computers

🔹 Create the following folder and files on the system where you’ll run the script:

C:\Logoff\

├── User.txt # List of usernames (e.g., jdoe, asmith)

├── computers.txt # List of computer names (e.g., DESKTOP-101)

└── Usernot exist.txt # This file will be created automatically for logging



📜 PowerShell Script: RemoteUserLogoff.ps1

Here’s the complete script that handles everything from pinging machines to logging off users and writing to a log file:


Here's a simple but powerful script:

# RemoteUserLogoff.ps1
$users = Get-Content -Path "C:\\Logoff\\User.txt"
$computers = Get-Content -Path "C:\\Logoff\\computers.txt"
$logFile = "C:\\Logoff\\Usernot exist.txt"
foreach ($computer in $computers) {
if (Test-Connection -ComputerName $computer -Count 1 -Quiet) {
Write-Host "$computer is online."
try {
$session = New-PSSession -ComputerName $computer -ErrorAction Stop
foreach ($user in $users) {
$userFound = Invoke-Command -Session $session -ScriptBlock {
param($user)
$userAccounts = query user
return $userAccounts -match $user
} -ArgumentList $user
if ($userFound) {
Write-Host "User $user is logged in on $computer. Attempting logoff..."
Invoke-Command -Session $session -ScriptBlock {
param($user)
$sessionList = query user
foreach ($line in $sessionList) {
if ($line -match $user) {
$sessionId = ($line -split '\\s+')[2]
logoff $sessionId
}
}
} -ArgumentList $user
}
else {
$message = "User account $user is not present in $computer"
Write-Host $message
Add-Content -Path $logFile -Value $message
}
}
Remove-PSSession $session
}
catch {
Write-Warning "Remote management is not enabled on $computer or access denied."
}
else {
Write-Warning "$computer is offline."
}
}

💡 Tip: Make sure you run PowerShell as Administrator to avoid permission issues.



📄 Sample Input Files

📂 User.txt

joe
alan
marvel

📂 computers.txt

joe_laptop01
joe_com03
desktop04

📈 Output

The script will:

  • Attempt to log off each user from each online machine

  • Skip offline machines or inaccessible hosts

  • Write a log to: C:\Logoff\Usernot exist.txt


Example log line:

User account joe is not present in DESKTOP04

🧾 Conclusion


This PowerShell script is a handy tool for IT administrators who need to automate remote user session management in Windows environments. It's fast, scalable, and completely customizable. Whether you're managing 5 machines or 500, this solution will save you time and help enforce better session hygiene.



📥 Download the Full Package

✅ Download :


📁 Inside the Package:

  • RemoteUserLogoff.ps1 – Full working script.

  • User.txt – Sample list of usernames.

  • computers.txt – Sample list of remote computers.

  • README.txt – Setup instructions and usage guide.


⚠️ Disclaimer

Important: This PowerShell script is provided for educational and administrative use. Before deploying it in a production environment, thoroughly test the script in a controlled test environment. This script performs user logoff operations and interacts with remote systems; improper use may cause data loss or session disruption. The author of this article is not responsible for any issues, data loss, or system impact that may occur from the use or misuse of this script.Administrators should review and execute it at their own risk, with proper change management and backups in place.

If you liked this article, do share the same. You can also buy me a Coffee using PayPal at "paypal.me/duttaavijit". This is purely a volunteer effort. THANK YOU !!!



bottom of page