Get Logged on Users from a List of Computer Names

10,642

Assuming the csv file contains a ComputerName header:

Import-Csv computers.csv | Foreach-Object{
    Get-WmiObject Win32_LoggedOnUser -ComputerName $_.ComputerName | Select-Object __SERVER,Antecedent -Unique | Foreach-Object {
     $domain,$user = [regex]::matches($_.Antecedent,'="([^"]+)"') | Foreach-Object {$_.Groups[1].Value}
     $_ | Select-Object __SERVER,@{Name='Domain';Expression={$domain}},@{Name='User';Expression={$user}}
    }
}
Share:
10,642
PowerShell
Author by

PowerShell

Think!

Updated on June 04, 2022

Comments

  • PowerShell
    PowerShell almost 2 years

    I wanted to extract a list of users logged on to remote pc, the ps names would be fed in using a .csv file.

    I was able to get a command

    Get-WmiObject Win32_LoggedOnUser -ComputerName $Computer | Select Antecedent -Unique
    

    to query the user names, could any one help me more on how to write this code?