≡ Menu

Powershell: How to logoff remote computer

In my one of my recent posts, I talked about shutdown the remote computer using Powershell. Since then I started exploring the other options available in Win32_OperatingSystem class and figured out one more helpful method which can logoff users logged on remote computers.

This script helps you to logoff the remote user who is current loogged on the computer. I didn’t test this against terminal services environment where multiple users are logged in, but you can try this and let me know how it goes.

Code:

[cmdletbinding()]            
            
param (            
            
 [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]            
 [string[]]$ComputerName = $env:computername,            
 [switch]$Force            
             
)            
            
begin {            
 $Username = $env:username            
             
 if($force) { $flag = 4 } else { $flag = 0 }             
 $comment  = "Logoff initiated by $Username using $($MyInvocation.InvocationName). Timeout is $Timeout"            
}            
            
process {            
 foreach($Computer in $ComputerName) {            
              
  Write-Verbose "Working on $Computer"            
  if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {            
   $OS  = Get-WMIObject -Class Win32_OperatingSystem -ComputerName $Computer            
   if( -not $OS.Win32ShutdownTracker(0, $comment, 0, $flag)) {            
    $Status  = "FAILED"            
   } else {            
    $Status  = "SUCCESS"            
   }            
  $OutputObj = New-Object -TypeName PSobject             
  $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer            
  $OutputObj | Add-Member -MemberType NoteProperty -Name LogOffStatus -Value $status            
  $OutputObj | Add-Member -MemberType NoteProperty -Name Timeout -Value $timeout            
  $OutputObj            
  }            
 }            
}            
            
end {            
}

Usage:

.\Logoff-Computer.ps1 -ComputerName MyPC1

Comments on this entry are closed.

  • Bryan May 24, 2012, 12:14 am

    Many tasks still seem much easier not using powershell…

    In a CMD window, use the following:
    Query session /server:[servername]

    Review the session you want to disconnect and type:
    logoff [Session ID#] /server:[servername]

    • Sitaram Pamarthi May 24, 2012, 8:01 am

      I won’t complete agree. If this is one time task for one time computer, may be command line tools are easier. But if you want to perform this regularly against many computers with status tracking, then you should rely on some or other scripting language. Powershell is current leader for windows scripting.

    • Steefa December 16, 2012, 10:44 am

      That’s an interesting and usfuel approach, although not quite what I was going for. My primary purpose was to make it easier to have a simple log file of what is happening. Leveraging the verbose pipeline was a bonus. I wasn’t trying to redirect or capture the Verbose, or any other stream. The lack of an option to capture the error, verbose and other pipelines is well known and perhaps we’ll see new options in some future version of PowerShell. But I do like that you added code to make this even more flexible. That’s always a good thing.

  • SMFX May 7, 2013, 9:43 pm

    Just bouncing around looking for ideas for a solution and came upon this script. Good idea, but the $Timeout variable is only referenced in output and is never used or given as input. It be perfectly useful if you changed the last param lines to:
    [switch]$Force,
    [ValidateRange(0,172800)][int]$Timeout=0
    )

    Then just change the Win32ShutdownTracker line to:
    if( -not $OS.Win32ShutdownTracker($Timeout, $comment, 0, $flag)) {

    Then you have a variable that actually means something.

    Thanks,
    -SMFX