<?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>find what computer a user is logged into powershell Archives - the Sysadmin Channel</title>
	<atom:link href="https://thesysadminchannel.com/tag/find-what-computer-a-user-is-logged-into-powershell/feed/" rel="self" type="application/rss+xml" />
	<link>https://thesysadminchannel.com/tag/find-what-computer-a-user-is-logged-into-powershell/</link>
	<description>Documenting My Life as a System Administrator</description>
	<lastBuildDate>Wed, 09 Feb 2022 03:14:26 +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 Logged In Users Using Powershell</title>
		<link>https://thesysadminchannel.com/get-logged-in-users-using-powershell/</link>
					<comments>https://thesysadminchannel.com/get-logged-in-users-using-powershell/#comments</comments>
		
		<dc:creator><![CDATA[Paul Contreras]]></dc:creator>
		<pubDate>Sun, 17 May 2020 05:16:09 +0000</pubDate>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Server Administration]]></category>
		<category><![CDATA[command to check users logged in windows remotely]]></category>
		<category><![CDATA[find all computers a user has logged into]]></category>
		<category><![CDATA[find what computer a user is logged into powershell]]></category>
		<category><![CDATA[find where a user is logged on in a domain]]></category>
		<category><![CDATA[get logged in user powershell]]></category>
		<category><![CDATA[powershell get logged on user on remote computer]]></category>
		<guid isPermaLink="false">https://thesysadminchannel.com/?p=2159</guid>

					<description><![CDATA[<p>A while back I posted my initial script to find the users that are logged into a server and log them off remotely. That was several years ago and I thought I would take another crack at it to try&#8230; <a href="https://thesysadminchannel.com/get-logged-in-users-using-powershell/" class="more-link">Continue Reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a href="https://thesysadminchannel.com/get-logged-in-users-using-powershell/">Get Logged In Users Using Powershell</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>A while back I posted my initial script to <a href="https://thesysadminchannel.com/find-users-logged-into-a-server-and-log-them-off-remotely/" rel="noopener noreferrer" target="_blank">find the users that are logged into a server</a> and log them off remotely.  That was several years ago and I thought I would take another crack at it to try to improve the speed, improve the formatting and add some dedicated switches with a bit more flexibility. Lastly, I wanted to change that ugly, unconventional name to something more Powershell approved.  I call this new function <strong>Get Logged In Users</strong>. </p>
<div id="tableofcontents">
<h2>Table Of Contents</h2>
<ul>
<li><a href="#LoggedInUsers">Get Logged In Users Using Powershell</a></li>
<ul>
<li><a href="#Parameters">Parameters</a></li>
<li><a href="#PowershellCode">Powershell Code</a></li>
</ul>
<li><a href="#HowToRunScript">How To Run Get Logged In User Powershell Script</a></li>
<li><a href="#QueryRemoteMachines">How To Query Remote Computers</a></li>
<li><a href="#Conclusion">Conclusion</a></li>
</ul>
</div>
<p>&nbsp;<br />
The underlying structure is still going to be using query session so that hasn&#8217;t changed.  What has changed is now we can specify a specific user or find where a user is logged on in a domain.  Another note is the ability to log them off using a simple switch so that can definitely come in handy if you&#8217;re checking a log of systems.</p>
<div id="LoggedInUsers" style="scroll-margin-top: 15px;"></div>
<h2>Get Logged In Users Using Powershell</h2>
<p>Looking back I don&#8217;t know why I added the ActiveDirectory Module and made it run as an administrator.  That is simply not needed and not required to run this function.  However, I would highly recommend you run this on a Windows 10 machine or Server 2016 and later with Powershell 5 because well let&#8217;s face it, we&#8217;re in 2020.</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: If the specified username is found logged into a machine, it will display it in the output.</p>
<div id="PowershellCode" style="scroll-margin-top: 15px;"></div>
<p>&nbsp;</p>
<pre class="brush: powershell; title: ; notranslate">

Function Get-LoggedInUser {
&lt;#
.SYNOPSIS
    This will check the specified machine to see all users who are logged on.
    For updated help and examples refer to -Online version.

.NOTES
    Name: Get-LoggedInUser
    Author: Paul Contreras
    Version: 3.0
    DateUpdated: 2021-Sep-21

.LINK
    https://thesysadminchannel.com/get-logged-in-users-using-powershell/ -
    For updated help and examples refer to -Online version.

.PARAMETER ComputerName
    Specify a computername to see which users are logged into it.  If no computers are specified, it will default to the local computer.

.PARAMETER UserName
    If the specified username is found logged into a machine, it will display it in the output.

.EXAMPLE
    Get-LoggedInUser -ComputerName Server01
    Display all the users that are logged in server01

.EXAMPLE
    Get-LoggedInUser -ComputerName Server01, Server02 -UserName jsmith
    Display if the user, jsmith, is logged into server01 and/or server02


#&gt;

    [CmdletBinding()]
        param(
            [Parameter(
                Mandatory = $false,
                ValueFromPipeline = $true,
                ValueFromPipelineByPropertyName = $true,
                Position=0
            )]
            [string[]] $ComputerName = $env:COMPUTERNAME,


            [Parameter(
                Mandatory = $false
            )]
            [Alias(&quot;SamAccountName&quot;)]
            [string]   $UserName
        )

    BEGIN {}

    PROCESS {
        foreach ($Computer in $ComputerName) {
            try {
                $Computer = $Computer.ToUpper()
                $SessionList = quser /Server:$Computer 2&gt;$null
                if ($SessionList) {
                    $UserInfo = foreach ($Session in ($SessionList | select -Skip 1)) {
                        $Session = $Session.ToString().trim() -replace '\s+', ' ' -replace '&gt;', ''
                        if ($Session.Split(' ')[3] -eq 'Active') {
                            [PSCustomObject]@{
                                ComputerName = $Computer
                                UserName     = $session.Split(' ')[0]
                                SessionName  = $session.Split(' ')[1]
                                SessionID    = $Session.Split(' ')[2]
                                SessionState = $Session.Split(' ')[3]
                                IdleTime     = $Session.Split(' ')[4]
                                LogonTime    = $session.Split(' ')[5, 6, 7] -as [string] -as [datetime]
                            }
                        } else {
                            [PSCustomObject]@{
                                ComputerName = $Computer
                                UserName     = $session.Split(' ')[0]
                                SessionName  = $null
                                SessionID    = $Session.Split(' ')[1]
                                SessionState = 'Disconnected'
                                IdleTime     = $Session.Split(' ')[3]
                                LogonTime    = $session.Split(' ')[4, 5, 6] -as [string] -as [datetime]
                            }
                        }
                    }

                    if ($PSBoundParameters.ContainsKey('Username')) {
                        $UserInfo | Where-Object {$_.UserName -eq $UserName}
                      } else {
                        $UserInfo | Sort-Object LogonTime
                    }
                }
            } catch {
                Write-Error $_.Exception.Message

            }
        }
    }

    END {}
}

</pre>
<p>&nbsp;</p>
<div id="HowToRunScript" style="scroll-margin-top: 15px;"></div>
<h2>How To Run Get Logged In User Powershell Script</h2>
<p>In order to the run the script there are a couple of things you need to do.  First and foremost, you need to <a href="https://thesysadminchannel.com/set-execution-policy-in-powershell/" target="_blank" rel="noopener noreferrer">set your execution policy</a> to RemoteSigned or bypass.  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 type: <strong>. .\_Scripts\Get-LoggedInUser.ps1</strong> &#8211; Note the two dots before the backslash.</li>
</ul>
<p>&nbsp;</p>
<div id="QueryRemoteMachines" style="scroll-margin-top: 15px;"></div>
<h2>Get Logged On Users On Remote Computers</h2>
<p>The best thing I love about this script is your ability to get who is logged into a remote computer.  This mitigates the need to physically log into computer and checking that way.  Since this Powershell script allows you to query remote servers and computers, it makes it highly automatable and very scalable.  Using this script, you can check 1 server or 1,000 servers and it would be the same amount of effort for the person who is running it.  It&#8217;s awesome and I love how you can do it all from your own Windows 10 computer.</p>
<p>You can also <strong>find where a user is logged on in a domain</strong> very easily.   Depending on how many machines you&#8217;re going to be iterating through, it can obviously take some time but it can be done.  Here is a simple code snippet of how to get where a user is logged in to.</p>
<pre class="brush: powershell; title: ; notranslate">
PS C:\&gt; . .\_Scripts\Get-LoggedInUser.ps1
PS C:\&gt;
PS C:\&gt; Get-LoggedInUser -ComputerName PAC-WIN1001, PAC-ADC01, PAC-DC01
 
ComputerName UserName   SessionState LogonTime
------------ --------   ------------ ---------
PAC-WIN1001  pcontreras Active       8/5/2021 10:00:00 PM
PAC-ADC01    pcontreras Disconnected 6/22/2021 7:27:00 AM
PAC-DC01     pcontreras Disconnected 6/22/2021 4:29:00 PM

</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2020/05/Get-Logged-In-User.png"><img fetchpriority="high" decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2020/05/Get-Logged-In-User.png" alt="Get-Logged In User" width="800" height="317" class="aligncenter size-full wp-image-3311" srcset="https://thesysadminchannel.com/wp-content/uploads/2020/05/Get-Logged-In-User.png?v=1628287856 800w, https://thesysadminchannel.com/wp-content/uploads/2020/05/Get-Logged-In-User-768x304.png?v=1628287856 768w" sizes="(max-width: 800px) 100vw, 800px" /></a></p>
<p>&nbsp;</p>
<div id="Conclusion" style="scroll-margin-top: 15px;"></div>
<h2>Conclusion</h2>
<p>So there you have it,  a quick and easy solution to a problem that many IT Administrators have come across in their sysadmin journey.  I am well aware that you can also do this via group policy if you wanted to go that route, or you can do this via some GUI tool but I always like to get my hands dirty with Powershell.</p>
<p>Thanks again for taking the time to visit and go ahead and drop a comment if you have any questions about the script or its intended fuctions.  Also, don&#8217;t forget to check out our <a href="https://www.youtube.com/c/theSysadminChannel" rel="noopener noreferrer" target="_blank">Youtube Channel</a> for visual video content and our own <a href="https://thesysadminchannel.com/powershell/" rel="noopener noreferrer" target="_blank">personal Powershell gallery</a> full of real world scripts.</p>
<p>The post <a href="https://thesysadminchannel.com/get-logged-in-users-using-powershell/">Get Logged In Users Using Powershell</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://thesysadminchannel.com/get-logged-in-users-using-powershell/feed/</wfw:commentRss>
			<slash:comments>12</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2159</post-id>	</item>
	</channel>
</rss>
