PowerShell: Disconnecting Users

What often gets old is when people disconnect from servers and leave files and folders locked, that could affect a release run.

nb: This isnt 100% pure powershell, but I am using powershell to perform the loop behaviour needed

foreach($item in (query user /server:$env:COMPUTERNAME 2>&1 | select -skip 1))
    $sessionId = ($item -split "\s+")[-5]
    Write-Verbose "Logging Off Session: $sessionId"
    logoff $sessionId /server:$env:COMPUTERNAME

query user /server:[Machinename] queries for user sessions, both connected and disconnected.

$env:COMPUTERNAME targets the current machine, you could pass a known machine name to the query.

2>&1 downgrades errors to information

This deals with any session, so even those currently connected, which adds even more fun!