<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>how to see who is logged into a server 2016 Archives - the Sysadmin Channel</title>
	<atom:link href="https://thesysadminchannel.com/tag/how-to-see-who-is-logged-into-a-server-2016/feed/" rel="self" type="application/rss+xml" />
	<link>https://thesysadminchannel.com/tag/how-to-see-who-is-logged-into-a-server-2016/</link>
	<description>Documenting My Life as a System Administrator</description>
	<lastBuildDate>Mon, 25 Oct 2021 20:39:04 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.2</generator>
<site xmlns="com-wordpress:feed-additions:1">144174110</site>	<item>
		<title>Get Users Logged Into Windows Server Remotely</title>
		<link>https://thesysadminchannel.com/find-users-logged-into-a-server-and-log-them-off-remotely/</link>
					<comments>https://thesysadminchannel.com/find-users-logged-into-a-server-and-log-them-off-remotely/#respond</comments>
		
		<dc:creator><![CDATA[Paul Contreras]]></dc:creator>
		<pubDate>Wed, 21 Mar 2018 01:37:47 +0000</pubDate>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Server Administration]]></category>
		<category><![CDATA[find where a user is logged on in a domain]]></category>
		<category><![CDATA[how to check users logged into windows server remotely]]></category>
		<category><![CDATA[how to see who is logged into a server 2012]]></category>
		<category><![CDATA[how to see who is logged into a server 2016]]></category>
		<category><![CDATA[log off user remotely]]></category>
		<category><![CDATA[log user off remotely]]></category>
		<category><![CDATA[log user off remotely powershell]]></category>
		<category><![CDATA[powershell list rdp sessions]]></category>
		<category><![CDATA[PowerShell script to Find RDP sessions on Servers]]></category>
		<category><![CDATA[remotely log user off]]></category>
		<category><![CDATA[see who is logged into a computer]]></category>
		<category><![CDATA[view remote desktop sessions server 2012]]></category>
		<category><![CDATA[view remote desktop sessions server 2016]]></category>
		<guid isPermaLink="false">https://thesysadminchannel.com/?p=327</guid>

					<description><![CDATA[<p>Update: This script has been updated and can be found here: Get Logged In Users Using Powershell Time and time again we have admins in our environment that connect to a remote desktop session, do what they need to do.&#8230; <a href="https://thesysadminchannel.com/find-users-logged-into-a-server-and-log-them-off-remotely/" class="more-link">Continue Reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a href="https://thesysadminchannel.com/find-users-logged-into-a-server-and-log-them-off-remotely/">Get Users Logged Into Windows Server Remotely</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><strong>Update:  This script has been updated and can be found here:  <a href="https://thesysadminchannel.com/get-logged-in-users-using-powershell/" rel="noopener noreferrer" target="_blank">Get Logged In Users Using Powershell</a></strong></p>
<p>Time and time again we have admins in our environment that connect to a remote desktop session, do what they need to do. And instead of logging off, they simply just close the RDP session. Leaving the user logged on until the cows come home.  I&#8217;ve come across this so many times in my environment (guilty of it myself) that I decided to put my Powershell abilities to work and create a <strong>script to remotely check who is logged into a server and log them off.</strong> This Powershell script gives you the option to see who is logged into a server and if you specified a username parameter it will <strong>Find Users Logged Into A Server</strong>.  It works perfect for Server 2012 R2 and Server 2016.</p>
<div id="tableofcontents">
<h2>Table Of Contents</h2>
<ul>
<li><a href="#Requirements">Prerequisites and Requirements</a></li>
<li><a href="#Parameters">Script Parameters</a></li>
<li><a href="#Examples">Examples and Usage</a></li>
<li><a href="#PowershellScript">Script Code &#8211; Get Users Logged Into Windows Machines</a></li>
<li><a href="#LogOffUsers">Log user off the entire domain</a></li>
<li><a href="#HowToRunScript">How to run the Script</a></li>
<li><a href="#VideoDemo">Video Demo Of Script</a></li>
</ul>
</div>
<div id="Requirements" style="scroll-margin-top: 15px;"></div>
<h2>Requirements</h2>
<p>In order to run this successfully, you need to have the following:</p>
<ul>
<li>The Active Directory Module must be installed on the computer.</li>
<li>Powershell Version 3.0 or greater.</li>
<li>Run the Powershell Windows as an administrator.</li>
</ul>
<p>The script actually will not run if the requirements are not met.</p>
<div id="Parameters" style="scroll-margin-top: 15px;"></div>
<h2>Parameters</h2>
<h3>    -Computername</h3>
<p>Description: This will specify the computername that you would like to check.  If no computername is specified, it will check the local computer.</p>
<h3>    -Username</h3>
<p>Description: This will specify a username that you would like to check.  If a username is specified, it will only filter said username.  The default is the currently logged on user.</p>
<h3>    -LogOff</h3>
<p>Description: If the logoff parameter is specified, it will logoff the user from the computer.</p>
<h3>    -LogErrors</h3>
<p>Description: If the LogErrors parameter is specified, it will log any errors that occurred during the run.  The default location is the users desktop.</p>
<p>&nbsp;</p>
<div id="Examples" style="scroll-margin-top: 15px;"></div>
<h2>Examples</h2>
<p><strong>Example 1: The default option is to check the current user and current computer.</strong></p>
<pre class="brush: powershell; title: ; notranslate">

PS C:\&gt; Get-UsersAndLogOffComputers

Computer  : PAC-WIN1001
Username  : pcontreras
State     : Active
Session   : console
SessionID : 2

</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers.png" target="_blank" rel="noopener noreferrer"><img fetchpriority="high" decoding="async" class="aligncenter wp-image-331 size-full" src="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers.png" alt="Get-UsersAndLogOffComputers" width="989" height="453" srcset="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers.png 989w, https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-768x352.png 768w" sizes="(max-width: 989px) 100vw, 989px" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>    Example 2: Specify a computername or multiple computernames seperated by a comma.</strong></p>
<pre class="brush: powershell; title: ; notranslate">

PS C:\&gt; Get-UsersAndLogOffComputers -ComputerName PAC-FS01, PAC-SCCM01

Computer  : PAC-FS01
Username  : pcontreras
State     : Active
Session   : rdp-tcp#8
SessionID : 3

Computer  : PAC-SCCM01
Username  : pcontreras
State     : Disconnected
Session   : Idle
SessionID : 3

</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName.png" target="_blank" rel="noopener noreferrer"><img decoding="async" class="aligncenter wp-image-333 size-full" src="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName.png" alt="Get-UsersAndLogOffComputers -ComputerName" width="992" height="473" srcset="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName.png 992w, https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName-768x366.png 768w" sizes="(max-width: 992px) 100vw, 992px" /></a></p>
<p>PAC-FS01 and PAC-SCCM01 were both specified and results were outputted. As you can see PAC-FS01 has an active session and PAC-SCCM01 has an Idle session. The Idle session means someone RDP&#8217;d in and disconnected instead of logging off.</p>
<p>&nbsp;</p>
<p><strong>    Example 3: Specify multiple computernames and a username.</strong></p>
<pre class="brush: powershell; title: ; notranslate">

PS C:\&gt; Get-UsersAndLogOffComputers -ComputerName PAC-FS01, PAC-SCCM01 -Username DJones

Computer  : PAC-FS01
Username  : DJones
State     : Active
Session   : console
SessionID : 1

</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName-Username.png" target="_blank" rel="noopener noreferrer"><img decoding="async" class="aligncenter wp-image-346 size-full" src="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName-Username.png" alt="Get-UsersAndLogOffComputers-ComputerName-Username" width="988" height="435" srcset="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName-Username.png 988w, https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName-Username-768x338.png 768w" sizes="(max-width: 988px) 100vw, 988px" /></a></p>
<p>The username DJones was specified and it was found logged into PAC-FS01. PAC-SCCM01 didn&#8217;t find a match so nothing was outputted.</p>
<p>&nbsp;</p>
<p><strong>    Example 4: Specify the username DJones and log it off the computer named PAC-FS01.</strong></p>
<pre class="brush: powershell; title: ; notranslate">

PS C:\&gt; Get-UsersAndLogOffComputers -ComputerName PAC-FS01 -Username DJones -Logoff

Computer  : PAC-FS01
Username  : DJones
State     : Active
Session   : console
SessionID : 1

</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName-Username-LogOff.png" target="_blank" rel="noopener noreferrer"><img decoding="async" class="aligncenter wp-image-354 size-medium" src="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName-Username-LogOff-1024x446.png" alt="Get-UsersAndLogOffComputers-ComputerName-Username-LogOff" width="1024" height="446" srcset="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName-Username-LogOff-1024x446.png 1024w, https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName-Username-LogOff-768x335.png 768w, https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName-Username-LogOff.png 1067w" sizes="(max-width: 1024px) 100vw, 1024px" /></a>The same output will occur with the -Logoff Switch, however it will log the specified user off as long as it has admin rights to that machine.</p>
<p>&nbsp;</p>
<p><strong>    Example 5: Log the errors (if any) to give you a better idea of what the issue is.</strong></p>
<pre class="brush: powershell; title: ; notranslate">

PS C:\&gt; Get-UsersAndLogOffComputers -ComputerName PAC-FS01, DoesntExist -Username DJones -LogErrors

</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName-Username-LogErrors.png" target="_blank" rel="noopener noreferrer"><img decoding="async" class="aligncenter wp-image-356 size-full" src="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName-Username-LogErrors.png" alt="Get-UsersAndLogOffComputers-ComputerName-Username-LogErrors - Find Users Logged Into A Server" width="998" height="272" srcset="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName-Username-LogErrors.png 998w, https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-ComputerName-Username-LogErrors-768x209.png 768w" sizes="(max-width: 998px) 100vw, 998px" /></a>In this example I specified the computer names PAC-FS01 and DoesntExist.  The username DJones doesn&#8217;t have rights to the server so the error that was logged shows Access is denied. The &#8220;Doesntexist&#8221; computer was not found and obviously not online so the error that was logged was &#8220;The RPC server is unavailable.&#8221;  The default path for the error log file is going to be the users desktop.</p>
<p>&nbsp;</p>
<div id="PowershellScript" style="scroll-margin-top: 15px;"></div>
<h2>Script To Get Users Logged Into Windows Server Remotely</h2>
<p>Great, so now that have the parameters and the examples out of the way, let&#8217;s get to the actual script.</p>
<pre class="brush: powershell; title: ; notranslate">

Function Get-UsersAndLogOffComputers {

#requires -Module ActiveDirectory
#requires -RunAsAdministrator
#requires -Version 3.0

&lt;#
.SYNOPSIS
    This will check to see if a user is logged on to a server and if specified, log them off.
    For updated help and examples refer to -Online version.

.DESCRIPTION
    This will check to see if a user is logged on to a server and if specified, log them off.
    For updated help and examples refer to -Online version.

.NOTES   
    Name: Get-UsersAndLogOffComputers
    Author: theSysadminChannel
    Version: 1.01
    DateCreated: 2017-Apr-01

.LINK
    https://thesysadminchannel.com/find-users-logged-into-a-server-and-log-them-off-remotely/ -
    For updated help and examples refer to -Online version.

#&gt;

    [CmdletBinding()]
        param(
            [Parameter(
                ValueFromPipeline=$true,
                ValueFromPipelineByPropertyName=$true,
                Position=0)]
            [string[]] $ComputerName = $env:COMPUTERNAME,

            [Parameter()]
            [string]   $Username = $env:USERNAME,

            [Parameter()]
            [switch]   $Logoff,

            [Parameter()]
            [switch]   $LogErrors
     
        )

    BEGIN {
        $ErrorLogFile = &quot;$env:USERPROFILE\Desktop\Get-UsersAndLogOffComputers.txt&quot;
        if (Test-Path $ErrorLogFile) {Remove-Item $ErrorLogFile}
    }

    PROCESS {
        Foreach ($Computer in $ComputerName) {
            try {
                $ExplorerProcess = Get-WmiObject Win32_Process -Filter &quot;Name = 'explorer.exe'&quot; -ComputerName $Computer -EA Stop
                    if ($ExplorerProcess) {
                        $ExplorerProcess = $ExplorerProcess.GetOwner().User
                        foreach ($Person in $ExplorerProcess) {
                            if ($Username -eq $Person) {
                                $Session = (query session $Username /Server:$Computer | Select-String -Pattern $Username -EA Stop).ToString().Trim()
                                $Session = $Session -replace '\s+', ' '
                                $Session = $Session -replace '&gt;', ''

                                    if ($Session.Split(' ')[2] -cne &quot;Disc&quot;) {
                                        $Properties = @{Computer  = $Computer
                                                        Username  = $Username.Replace('{}','')
                                                        Session   = $Session.Split(' ')[0]
                                                        SessionID = $Session.Split(' ')[2]
                                                        State     = $Session.Split(' ')[3]
                                                        }
                                    } else {
                                        $Properties = @{Computer  = $Computer
                                                        Username  = $Username.Replace('{}','')
                                                        Session   = 'Idle'
                                                        SessionID = $Session.Split(' ')[1]
                                                        State     = 'Disconnected'
                                                        }
                        
                                    }
                                $Object = New-Object -TypeName PSObject -Property $Properties | Select Computer, Username, State, Session, SessionID
                            } 
                        }
                    } 

            } catch { 
                $ErrorMessage = $Computer + &quot; Error: &quot; + $_.Exception.Message
                Write-Error $_.Exception.Message

            } finally {
                if ($ErrorMessage -and $LogErrors) {
                        Write-Output $ErrorMessage | Out-File $ErrorLogFile -Append
                        $ErrorMessage = $null
                }

                if ($Logoff -and $Object.SessionID) {
                    LogOff.exe /server:$Computer $Object.SessionID
                }

            Write-Output $Object
            $Object = $null
            }
        }
    }

    END {}

}

</pre>
<p>&nbsp;</p>
<div id="LogOffUsers" style="scroll-margin-top: 15px;"></div>
<h2>Log user off the entire domain or just all servers</h2>
<p>Since this script allows inputs for multiple objects, we can easily pipe in an array of Servers or computers in general.</p>
<p>Getting all the Servers in the domain and seeing if the user DJones is logged on.  Again we can use the <code>-LogOff</code> parameter to log the user out.  If you have more 100 or 200 hundred servers I would probably make the filter a little smaller so use with a bit of caution.</p>
<pre class="brush: powershell; title: ; notranslate">
#Get all servers and enumerate through each one to see if the user is logged in.
$Servers = Get-ADComputer -Filter {(OperatingSystem -like &quot;*Server*&quot;) -and (Enabled -eq $true)} -Properties OperatingSystem | select -ExpandProperty Name | Sort-Object
Get-UsersAndLogOffComputers -ComputerName $Servers -Username DJones

#Get every computer object in the domain that is enabled and check to see if the user is logged in. 
$AllComputers = Get-ADComputer -Filter {Enabled -eq $true} | select -ExpandProperty Name | Sort-Object
Get-UsersAndLogOffComputers -ComputerName $AllComputers -Username DJones
</pre>
<p>&nbsp;</p>
<div id="HowToRunScript" style="scroll-margin-top: 15px;"></div>
<h2>How to run the Script</h2>
<p>In order to the run the script there are a couple of things you need to do.  First and foremost, <a href="https://thesysadminchannel.com/set-execution-policy-in-powershell/" target="_blank" rel="noopener noreferrer">set your execution policy</a> to RemoteSigned.  This is a standard with running <em>any</em> powershell script.</p>
<p>Next you need to dot source the script since it is a function.  To dot source the script do the following:</p>
<ul>
<li>Copy the script above and save it any location. In this example I&#8217;ll save it to my C:\_Scripts folder.</li>
<li>Within the Powershell Window running as Administrator type: <strong>. .\_Scripts\Get-UsersAndLogOffComputers.ps1 </strong>&#8211; Note the two dots before the backslash.</li>
</ul>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-DotSource.png" target="_blank" rel="noopener noreferrer"><img decoding="async" class="aligncenter wp-image-367 size-full" src="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-DotSource.png" alt="Get-UsersAndLogOffComputers DotSource - Find Users Logged Into A Server" width="938" height="431" srcset="https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-DotSource.png 938w, https://thesysadminchannel.com/wp-content/uploads/2018/04/Get-UsersAndLogOffComputers-DotSource-768x353.png 768w" sizes="(max-width: 938px) 100vw, 938px" /></a></p>
<p>Now you&#8217;re all set to call the function and run any of the parameters listed above.</p>
<div id="VideoDemo" style="scroll-margin-top: 15px;"></div>
<h2>Video and Explanation of how to Find Users Logged Into A Server</h2>
<p><iframe title="Log off Users Remotely - Check Who Is Logged Into Servers" width="640" height="360" src="https://www.youtube.com/embed/KzcqShaGpoo?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe><br />
&nbsp;</p>
<p>If you have any trouble dot sourcing the script or find any bugs please let me know in the comments section and I&#8217;ll be happy to update it.</p>
<p>&nbsp;</p>
<p>If you&#8217;re looking to expand your Powershell knowledge check out <a href="https://amzn.to/2HoPSfw" target="_blank" rel="noopener noreferrer">Don Jones Learn Powershell In a Month Of Lunches Book</a> and I hope this article to Find Users Logged Into A Server was informative and if you need to log user off remotely hopefully you will use this script.</p>
<p>The post <a href="https://thesysadminchannel.com/find-users-logged-into-a-server-and-log-them-off-remotely/">Get Users Logged Into Windows Server Remotely</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://thesysadminchannel.com/find-users-logged-into-a-server-and-log-them-off-remotely/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">327</post-id>	</item>
	</channel>
</rss>
