Check if a user is logged-in with PowerShell

Patching machines is usually a constraint for the end user, mainly because of the reboot required at the end of the update…

A good workaround to update machines without disrupting the user is to apply the patch and then reboot when no user is logged on the machine.

Many possibilities exist to check whether a user is logged on a machine or not, one of them is to check if an “Explorer” process is running on the machine.

Here is a PowerShell script allowing to achieve this:

# Check if a user is logged-in
Write-Host "Check if a user is connected on the machine"

$ExplorerProcess = get-process | where {$_.ProcessName -eq "explorer"}
$ExplorerProcess = $ExplorerProcess.ProcessName

If ($ExplorerProcess -eq "explorer") {
	Write-Host "A user is connected"
Else {
	Write-Host "No user connected"