Which User is Logged in to Workstation

It is useful to know which user is logged in to the specified workstation.

For this I wrote this script.

#getting the needed workstations. in this example all XPs in our domain
Get-QADComputer -OSName "Windows XP Professional" | foreach `
    {
#each of them is testing for ping. if it's pinging the script continue
    if (Test-Connection $_.name -Quiet)
        {
        $name = $_.name
#generates a string in which is workstation name; username; user's email        
        $name + ";" + `
        ((Get-WmiObject Win32_ComputerSystem -ComputerName $name -EA Stop).UserName) + ";" + `
        (Get-QADUser (((Get-WmiObject Win32_ComputerSystem -ComputerName $name -EA Stop).UserName)) -EA Stop | foreach {$_.email})
#this string is for trapping errors generating by last string and output workstation's name; error message
        trap {$name + ";" + "{0}" -f $_.Exception.Message; continue}
        }
    else
        {
#if the workstation do not pinging there are an output string with name;don't respond
        $_.name + ";don't respond"
        }
    }

It’s is possible to add >> c:\file.csv and import this list to excel.

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark
tabs-top

2 Responses to “Which User is Logged in to Workstation”

  1. Martin says:

    HI Daniel

    I’ve made some small changes to the script but what I do to export the info I need from the script is this:

    $outfile=”\\server\c$\powershell\$name\LoggedInUsers.txt”

    $Result = Test-Path -path “\\server\c$\powershell\$name”
    if ($result -eq $False)
    {
    new-item -ItemType Directory -Force -Path “\\server\c$\powershell\$name”
    Write-output “$name, $username” | out-File $outfile -append
    }
    Else
    {Write-output “$name, $username” | out-File $outfile -append }

  2. Daniel says:

    I didn’t manage to export to Excel/CSV. :(
    Where wo I have to place my “| Export-CSV…” statement?

Leave a Reply