Over a million developers have joined DZone.

How to Retrieve the Logged on User of a Remote Machine in Powershell

DZone's Guide to

How to Retrieve the Logged on User of a Remote Machine in Powershell

Want to know how to quickly retrieve the logged on user of a remote Windows machine/server? Check out this post.

· DevOps Zone ·
Free Resource

Is the concept of adopting a continuous everything model a daunting task for your fast moving business? Read this whitepaper to break down and understand one of the key pillars of this model in Continuous Governance: The Guardrails for Continuous Everything.

A good way to retrieve users of remote machines could be to filter the process for explorer.exe:

Get-WmiObject -class win32_process -Filter "name = 'Explorer.exe'" -ComputerName MACHINENAME -EA "Stop" | % {$_.GetOwner().User}

For all logged on users though, use the following script: https://gallery.technet.microsoft.com/scriptcenter/d46b1f3b-36a4-4a56-951b-e37815a2df0c

function Get-LoggedOnUser {
#Requires -Version 2.0          
   )#End Param

Write-Host "`n Checking Users . . . "
$i = 0          
    $ComputerName | Foreach-object {
    $Computer = $_
            $processinfo = @(Get-WmiObject -class win32_process -ComputerName $Computer -EA "Stop")
                if ($processinfo)
                    $processinfo | Foreach-Object {$_.GetOwner().User} |
                    Where-Object {$_ -ne "NETWORK SERVICE" -and $_ -ne "LOCAL SERVICE" -and $_ -ne "SYSTEM"} |
                    Sort-Object -Unique |
                    ForEach-Object { New-Object psobject -Property @{Computer=$Computer;LoggedOn=$_} } |
                    Select-Object Computer,LoggedOn
            "Cannot find any processes running on $computer" | Out-Host




Are you looking for greater insight into your software development value stream? Check out this whitepaper: DevOps Performance: The Importance of Measuring Throughput and Stability to see how CloudBees DevOptics can give you the visibility to improve your continuous delivery process.

powershell ,script ,remote ,windows

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}