How to Use PowerShell to Get the Current Logged On User on a Remote Computer?

In this tutorial, I will explain how to use PowerShell to get the current logged-on user on a remote computer. By the end of this post, you’ll be able to use PowerShell commands to identify logged-on users on remote machines.

PowerShell to Get the Current Logged On User on a Remote Computer

Now, let me show you how to get the current logged-on user on a remote computer using PowerShell.

Note: You need to have administrative privileges on the remote computer.

Enable PowerShell Remoting

To enable PowerShell Remoting, run the following command on both the local and remote computers:

Enable-PSRemoting -Force

This command configures the necessary firewall rules and starts the WinRM service required for remote PowerShell sessions.

Basic Command to Get Logged On User

To get the current logged-on user on a remote computer, you can use the Get-WmiObject cmdlet. Here’s a simple command to retrieve this information:

Get-WmiObject -Class Win32_ComputerSystem -ComputerName REMOTE_COMPUTER_NAME | Select-Object UserName

Replace REMOTE_COMPUTER_NAME with the name of the remote computer. This command queries the Win32_ComputerSystem class and retrieves the UserName property, which contains the name of the currently logged-on user.

Check out How to Keep Your Screen Active with a PowerShell Script?

Example Scenario

Imagine you are an IT administrator at a company in New York, managing a network of computers. One of your tasks is to ensure that users are logged off at the end of the day to save energy and comply with security policies. You need to check if users are still logged on to their machines remotely.

Here’s how you can do it using PowerShell:

  1. Open PowerShell on your local machine.
  2. Run the following command to get the logged-on user on a remote computer named NYC-PC01:
Get-WmiObject -Class Win32_ComputerSystem -ComputerName NYC-PC01 | Select-Object UserName

If NYC-PC01 has a user logged in, you will see an output like this:

UserName
--------
NYC\JohnDoe

Advanced Methods

While the basic method works well, there are more advanced techniques to gather additional details or handle multiple computers at once.

Using CIMCmdlets

The Get-CimInstance cmdlet is a modern alternative to Get-WmiObject and is generally faster and more efficient. Here’s how to use it:

Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName NYC-PC01 | Select-Object UserName

Querying Multiple Computers

To query multiple remote computers, you can use a loop. Here’s an example:

$computers = @('NYC-PC01', 'NYC-PC02', 'NYC-PC03')

foreach ($computer in $computers) {
    $user = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computer | Select-Object UserName
    Write-Output "$computer: $($user.UserName)"
}

This script will loop through each computer in the $computers array and print the logged-on user for each one.

Check out Create Local Users in Windows Using PowerShell

Handling Errors

You might encounter errors with remote computers due to network issues, permissions, or other factors. It’s essential to handle these errors gracefully. Here’s an improved script that includes error handling:

$computers = @('NYC-PC01', 'NYC-PC02', 'NYC-PC03')

foreach ($computer in $computers) {
    try {
        $user = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computer -ErrorAction Stop | Select-Object UserName
        Write-Output "$computer: $($user.UserName)"
    } catch {
        Write-Output "Failed to retrieve user for $computer: $_"
    }
}

Example

You need to ensure that all employees have logged off their computers at the end of the workday. You can create a PowerShell script that runs at a specific time to check the logged-on users and send you a report.

Here’s an example script:

$computers = Get-Content -Path 'C:\scripts\computers.txt'
$report = @()

foreach ($computer in $computers) {
    try {
        $user = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computer -ErrorAction Stop | Select-Object UserName
        $report += [PSCustomObject]@{
            ComputerName = $computer
            UserName     = $user.UserName
        }
    } catch {
        $report += [PSCustomObject]@{
            ComputerName = $computer
            UserName     = 'Failed to retrieve user'
        }
    }
}

$report | Export-Csv -Path 'C:\scripts\LoggedOnUsersReport.csv' -NoTypeInformation

This script reads a list of computer names from a file, checks the logged-on user for each computer, and exports the results to a CSV file.

How to Use PowerShell to Get the Current Logged On User on a Remote Computer

Conclusion

In this tutorial, I explained how to get the current logged-on user on a remote computer using PowerShell.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.