Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / Find the Current User Logged on a Remote Computer

October 18, 2021 Active DirectoryPowerShellWindows 10Windows Server 2019

Find the Current User Logged on a Remote Computer

Quite often an administrator needs to quickly find out the username logged on a remote Windows computer. In this article, we’ll show some tools and PowerShell scripts that can help you to get the names of users logged on the remote computer.

Contents:
  • Check Logged in Users with PSLoggedOn and Qwinsta
  • How to Get the Current User on a Remote Computer Using PowerShell?
  • PowerShell Script to Find Logged On Users on Remote Computers

Check Logged in Users with PSLoggedOn and Qwinsta

Microsoft’s SysInternals PSTools includes a console utility called PSLoggedOn.exe that can be used to get the name of the user who is logged into a remote computer, as well as a list of SMB sessions connected to it.

Download the tool and run it:

psloggedon \\RemoteCompName

psloggedon View Whom Is Logged on remote computer

As you can see, the tool returned the name of the logged-on user (Users logged on locally) and a list of users who access this computer’s SMB resources over the network (Users logged on via resource shares).

If you want to get only the name of the user logged on locally, use the -l option:

Psloggedon.exe \\pc1215wks1 –l

PSLoggedOn connects to the registry and checks the name of the user logged on locally. To do it, the RemoteRegistry service must be running. You can run and configure automatic service startup using PowerShell:

Set-Service RemoteRegistry –startuptype automatic –passthru
Start-Service RemoteRegistry

You can also get a list of sessions on a remote computer using the built-in qwinsta tool. This tool should be familiar to any administrator managing Remote Desktop Services (RDS) terminal environment. To get a list of logged user sessions from a remote computer, run the command:

qwinsta /server:be-rdsh01

qwinsta - find a logged on user remotely

The tool returns a list of all sessions (active and disconnected by an RDP timeout) on an RDS server or in a desktop Windows 10 (11) edition (even if you allowed multiple RDP connections to it).

If you get the Error 5 Access Denied when trying to connect to a remote server using qwinsta, make sure that the remoter host is allowed to remotely manage users via RPC. If needed, enable it in the registry using the following command or using GPO:

reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v "AllowRemoteRPC" /t "REG_DWORD" /d "1" /f

How to Get the Current User on a Remote Computer Using PowerShell?

You can get the name of the user logged on to the computer using the Win32_ComputerSystem WMI class. Open the PowerShell console and run the command:

Get-WmiObject -class Win32_ComputerSystem | Format-List Username

The command returns the name of the user logged on to the computer.

Get-WMIObject Win32_ComputerSystem UserName

The Get-WmiObject cmdlet has the –ComputerName option you can use to access WMI objects on a remote computer. The following command will return the logged-in username from the remote computer:

(Get-WmiObject -class Win32_ComputerSystem –ComputerName pc1215wks1).Username

get remote logged on user with powershell

The command shows only the user logged on to the console (not through the RDP).

You can also use PSRemoting to get information from remote computers with the Invoke-Command cmdlet.

YOu can get only the username on the remote computer (without a domain), use these commands:

$userinfo = Get-WmiObject -ComputerName pc1215wks1 -Class Win32_ComputerSystem
$user = $userinfo.UserName -split '\\'
$user[1]

In modern PowerShell Core (pwsh.exe) versions, you need to use the Get-CimInstance cmdlet instead of Get-WmiObject:

Get-CimInstance –ComputerName pc1215wks1 –ClassName Win32_ComputerSystem | Select-Object UserName

Or:

(Get-CimInstance -ComputerName pc1215wks1 -ClassName Win32_ComputerSystem).CimInstanceProperties | where{$_.Name -like "UserName"}| select value

Get-CimInstance - see currently logged in users

GetCiminstance uses WinRM to connect to remote computers so you have to enable and configure WinRM on them using GPO or the following command:

WinRM quickconfig

PowerShell Script to Find Logged On Users on Remote Computers

If you want to collect information about logged-in users from multiple computers, you can use the following PowerShell function to get usernames.

function Get-LoggedUser
{
    [CmdletBinding()]
    param
    (
        [string[]]$ComputerName 
    )
    foreach ($comp in $ComputerName)
    {
        $output = @{'Computer' = $comp }
        $output.UserName = (Get-WmiObject -Class win32_computersystem -ComputerName $comp).UserName
        [PSCustomObject]$output
    }
}

Specify the names of computers you want to check usernames on with Get-LoggedUser:

Get-LoggedUser pc1215wks1,pc1215wks2,mun-dc01

Powershell script to view logged on users on multiple AD computers

If the function returns an empty username for a computer, it means that nobody is logged on.

You can get the names of users logged on the computers in an Active Directory domain. Use the Get-ADComputer cmdlet to get the list of computers in the domain. In the example below, we will get the usernames logged on active computers in the specific domain OU. In order to make the script work faster prior to accessing a remote computer, I added a check of its availability using ICMP ping and the Test-NetConnection cmdlet:

function Get-LoggedUser
{
    [CmdletBinding()]
    param
    (
        [string[]]$ComputerName 
    )
    foreach ($comp in $ComputerName)
    {
        if ((Test-NetConnection $comp -WarningAction SilentlyContinue).PingSucceeded -eq $true) 
            {  
                $output = @{'Computer' = $comp }
                $output.UserName = (Get-WmiObject -Class win32_computersystem -ComputerName $comp).UserName
            }
            else
            {
                $output = @{'Computer' = $comp }
                         $output.UserName = "offline"
            }
         [PSCustomObject]$output 
    }
}
$computers = (Get-AdComputer -Filter {enabled -eq "true"} -SearchBase 'OU=Berlin,DC=woshub,DC=com').Name
Get-LoggedUser $computers |ft -AutoSize

Get Logged In Users with Powershell Script

Also, note that you can store the name of the logged-on user in the computer properties in AD. To do it, you can use a logon script described in the article “Set-ADComputer: How to Add User Information to AD Computer Properties”.

After that, you don’t need to scan all computers to find where a specific user is logged on. You can find a user computer using a simple query to Active Directory:

$user='m.smith'
$user_cn=(Get-ADuser $user -properties *).DistinguishedName
Get-ADComputer -Filter "ManagedBy -eq '$user_cn'" -properties *|select name,description,managedBy|ft

1 comment
0
Facebook Twitter Google + Pinterest
previous post
How to Install and Use ClamAV Antivirus on CentOS/RHEL?
next post
Black Screen While Using Windows Remote Desktop (RDP) Connection

Related Reading

How to Deploy Windows 10 (11) with PXE...

June 27, 2022

Checking Windows Activation Status on Active Directory Computers

June 27, 2022

Configuring Multiple VLAN Interfaces on Windows

June 24, 2022

How to Disable or Enable USB Drives in...

June 24, 2022

Adding Domain Users to the Local Administrators Group...

June 23, 2022

1 comment

Stach October 28, 2021 - 7:03 am

Hi, I think the easiest way to get logged user, is to use command “Query user /server:PCNAME”

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows 7
  • Windows Server 2019
  • Windows Server 2016
  • Windows Server 2012 R2
  • PowerShell
  • VMWare
  • Hyper-V
  • MS Office

Recent Posts

  • How to Deploy Windows 10 (11) with PXE Network Boot?

    June 27, 2022
  • Checking Windows Activation Status on Active Directory Computers

    June 27, 2022
  • Configuring Multiple VLAN Interfaces on Windows

    June 24, 2022
  • How to Disable or Enable USB Drives in Windows using Group Policy?

    June 24, 2022
  • Adding Domain Users to the Local Administrators Group in Windows

    June 23, 2022
  • Viewing a Remote User’s Desktop Session with Shadow Mode in Windows

    June 23, 2022
  • How to Create a Wi-Fi Hotspot on your Windows PC?

    June 23, 2022
  • Configuring SSH Public Key Authentication on Windows

    June 15, 2022
  • How to Run a Program as a Different User (RunAs) in Windows?

    June 15, 2022
  • FAQ: Licensing Microsoft Exchange Server 2019/2016

    June 14, 2022

Follow us

woshub.com

ad

  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • Allow RDP Access to Domain Controller for Non-admin Users
  • How to Find the Source of Account Lockouts in Active Directory domain?
  • How to Disable or Enable USB Drives in Windows using Group Policy?
  • Deploy PowerShell Active Directory Module without Installing RSAT
  • Configuring Proxy Settings on Windows Using Group Policy Preferences
  • Managing User Photos in Active Directory Using ThumbnailPhoto Attribute
Footer Logo

@2014 - 2018 - Windows OS Hub. All about operating systems for sysadmins


Back To Top