Skip to content

Instantly share code, notes, and snippets.

@jhochwald
Created January 6, 2020 23:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhochwald/c95f3b249e9c23abe9825f80726884a7 to your computer and use it in GitHub Desktop.
Save jhochwald/c95f3b249e9c23abe9825f80726884a7 to your computer and use it in GitHub Desktop.
Log off all users on a Windows Server or Remote Desktop Server
<#
.SYNOPSIS
Log off all users on a Windows Server or Remote Desktop Server
.DESCRIPTION
Log off all users on a Windows Server or Remote Desktop Server
.PARAMETER ComputerName
Specifies the computers on which the command runs. The default is the local computer.
When you use the ComputerName parameter, Windows PowerShell creates a temporary connection that is used only to run the specified command and is then closed. If you need a persistent connection, use the Session parameter.
Type the NETBIOS name, IP address, or fully qualified domain name of one or more computers in a comma-separated list. To specify the local computer,
.EXAMPLE
PS C:\> .\Invoke-LogOffAllUsersOnServer.ps1
Log off all users on the local Windows Server
.EXAMPLE
PS C:\> .\Invoke-LogOffAllUsersOnServer.ps1 -ComputerName 'FFMRDS01'
Log off all users on the Remote Desktop Server 'FFMRDS01'
.EXAMPLE
PS C:\> .\Invoke-LogOffAllUsersOnServer.ps1 -ComputerName 'FFMAPP01','NYCRDS01'
Log off all users on the remote Windows Server 'FFMAPP01' and the Remote Desktop Server 'NYCRDS01'
.NOTES
Initial release.
.LINK
https://github.com/jhochwald/PowerShell-collection/
#>
[CmdletBinding(ConfirmImpact = 'Medium',
SupportsShouldProcess)]
param
(
[Parameter(ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string[]]
$ComputerName = $env:COMPUTERNAME
)
begin
{
#region Defaults
$RunVerbose = ($PSCmdlet.MyInvocation.BoundParameters['Verbose'].IsPresent)
if (-not $RunVerbose)
{
$RunVerbose = $false
}
#endregion Defaults
#region ScriptBlock
$ScriptBlock = {
# How do we handle Errors in this ScriptBlock?
$ErrorActionPreference = 'Continue'
# Find all sessions and filter them with the query command
$UserSessions = & "$env:windir\system32\query.exe" session | Where-Object -FilterScript {
$_ -notmatch '^ SESSIONNAME'
} | ForEach-Object -Process {
$SessionIDs = '' | Select-Object -Property 'Id'
$SessionIDs = $_.Substring(39, 9).Trim()
$SessionIDs
}
# Now we loop over the list off
foreach ($UserSession in $UserSessions)
{
# Use the LogOfff command
$null = (& "$env:windir\system32\logoff.exe" $UserSession)
}
}
#endregion ScriptBlock
}
process
{
foreach ($SingleComputerName in $ComputerName)
{
if ($PSCmdlet.ShouldProcess($SingleComputerName, 'Log off all users'))
{
# Run the scriptblock's code on the remote computer
$paramInvokeCommand = @{
ComputerName = $SingleComputerName
ScriptBlock = $ScriptBlock
ErrorAction = 'Continue'
Verbose = $RunVerbose
}
$null = (Invoke-Command @paramInvokeCommand)
}
}
}
#region LICENSE
<#
Copyright (c) 2020, enabling Technology. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
By using the Software, you agree to the License, Terms and Conditions above!
#>
#endregion LICENSE
#region DISCLAIMER
<#
DISCLAIMER:
- Use at your own risk, etc.
- This is open-source software, if you find an issue try to fix it yourself. There is no support and/or warranty in any kind
- This is a third-party Software
- The developer of this Software is NOT sponsored by or affiliated with Microsoft Corp (MSFT) or any of its subsidiaries in any way
- The Software is not supported by Microsoft Corp (MSFT)
- By using the Software, you agree to the License, Terms, and any Conditions declared and described above
- If you disagree with any of the Terms, and any Conditions declared: Just delete it and build your own solution
#>
#endregion DISCLAIMER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment