Hi All,
I am trying to export which user is logged in on which machine, via PC names.
Any idea why below script is not working ? I am stumped 
$computername = Get-Content computers.txt
foreach ($pc in $computername){
$logged_in = (Get-WmiObject -class win32_process -ComputerName $pc | Where-Object name -Match explorer).getowner().user
$name = $logged_in.split("\")[1]
"{0}: {1}" -f $pc,$name | Out-File -Append User_On_PC.txt
}
Many Thanks.
7 Spice ups
Neally
(Neally)
2
Maybe something like that?
Foreach($computername in (Get-Content computers.txt)){
$loggedusers = Invoke-Command -ComputerName $computername -ScriptBlock{
quser
}
Write-Output ($computername + ":") | Out-File c:\somepath\file.txt -Append
Write-Output $loggedusers | Out-File c:\somepath\file.txt -Append
}
1 Spice up
Neally
(Neally)
3
If you run this for a single machine,
(Get-WmiObject -class win32_process -ComputerName $insertNameHere | Where-Object name -Match explorer).getowner().user
^ does this actually give you a ‘domain\username’ output? I just get the ‘username’
If you try to split on the and then just take the second ( [1] ) arrayobject, since it did not split, it might be blank.
(Get-WmiObject -class win32_process -ComputerName 'localhost' | Where-Object name -Match explorer).getowner().user
This only gives me the current user without the domain. So there is nothing to split afterwards on the next line.
jitensh
(JitenSh)
5
simply
$comp=get-content C:\computers.txt
Get-WMIObject -class Win32_ComputerSystem -ComputerName $comp | select username,pscomputername
or use SoftPerfect Network Scanner : fast, flexible, advanced
2 Spice ups
britv8
(britv8)
6
foreach ($item in $Output)
{
# check online status of computer
If ((Test-Connection -ComputerName $item.Computername -Count 1 -Quiet) -eq $true)
{
Try # grab machine and logged on User
{
Get-WmiObject -ComputerName "$($item.Computername)" -Namespace root\cimv2 -Class Win32_ComputerSystem -ErrorAction Stop| % {"$($_.Name): $($_.username)"}
} # Move to next computer
Catch
{
Write-Host "$($item.Computername): COMMUNICATION ISSUE" -foregroundcolor red #current user running script does not have administrator rights
}
}
Else
{
Write-Host "$($item.Computername): OFFLINE" -foregroundcolor yellow
Continue # Move to next computer
}
}
Currently using this
britv8
(britv8)
7
Why my script does not check for is users that have been switched out it only checks the current logged in user
Get-CIMInstance can indicate other logged in users as the user profile is in use (loaded )
PSTerminalServices module also works on PCs and can get those sessions that are disconnected on a PC
https://psterminalservices.codeplex.com/
1 Spice up
craig582
(Craig582)
8
I was looking for a similar thing a while back.
1 Spice up
You can track users logon session as well as logged-In machine through event logs.
Please check this How-To for needful steps - https://community.spiceworks.com/how_to/130398-how-to-track-user-logon-sessions-using-event-log
1 Spice up
@ Neally - Invoke command does not work as Psremoting is disabled 
@britv8 - your script doesn’t work for me mate, i modified it though;
$computer = Get-Content computers.txt
foreach ($item in $computer)
{
# check online status of computer
If ((Test-Connection -ComputerName $item.Computername -Count 1 -Quiet) -eq $true)
{
Try # grab machine and logged on User
{
Get-WmiObject -ComputerName "$($item.Computername)" -Namespace root\cimv2 -Class Win32_ComputerSystem -ErrorAction Stop| % {"$($_.Name): $($_.username)"| Out-File -Append User_On_PC.txt}
} # Move to next computer
Catch
{
Write-Host "$($item.Computername): COMMUNICATION ISSUE" -foregroundcolor red| Out-File -Append Issue_On_PC.txt #current user running script does not have administrator rights
}
}
Else
{
Write-Host "$($item.Computername): OFFLINE" -foregroundcolor yellow | Out-File -Append PC_Offline.txt
Continue # Move to next computer
}
}
@ Jitensh - Yours works alright, with most PC’s. For some PC’s i get blank as a username.
Thanks all for your help though.
@alexw @britv8 @jitensh
Neally
(Neally)
11
Well if Jiten’s works, add validation, it is most likely blank if it can not connect to the PC.
jitensh
(JitenSh)
12
also you can use
Get-WmiObject -Class win32_process -ComputerName $computername |
Where-Object{ $_.Name -eq "explorer.exe" } |
ForEach-Object{ ($_.GetOwner()).Domain + "\" + ($_.GetOwner()).User; }
britv8
(britv8)
13
becasue you are using get-content and not a CSV you will need to replace $Item.Computername with $item throughout my script
try this
$computer = Get-Content computers.txt
foreach ($item in $computer)
{
# check online status of computer
If ((Test-Connection -ComputerName $item -Count 1 -Quiet) -eq $true)
{
Try # grab machine and logged on User
{
Get-WmiObject -ComputerName "$item" -Namespace root\cimv2 -Class Win32_ComputerSystem -ErrorAction Stop| % {"$($_.Name): $($_.username)"| Out-File -Append User_On_PC.txt}
} # Move to next computer
Catch
{
Write-Host "$item : COMMUNICATION ISSUE" -foregroundcolor red| Out-File -Append Issue_On_PC.txt #current user running script does not have administrator rights
}
}
Else
{
Write-Host "$item : OFFLINE" -foregroundcolor yellow | Out-File -Append PC_Offline.txt
Continue # Move to next computer
}
}
1 Spice up
@ britv8 - cheers bud - quick one, do you know why below results are not exporting ?
Write-Host "$item : OFFLINE" -foregroundcolor yellow | Out-File -Append PC_Offline.txt
Write-Host "$item : COMMUNICATION ISSUE" -foregroundcolor red | Out-File -Append Issue_On_PC.txt
It seems out-file is not working for PC’s offline.
@JitenSh - i like what you did there, how can I replace
$_.GetOwner()).Domain
with
$_.GetOwner()).Computername
Then it be perfect.
@ Neally - I found the reason i was getting blank. Because PC was sitting on lock screen and no one had logged into it.
Thanks All
@alexw @britv8 @jitensh
britv8
(britv8)
15
As you are really wanting to output to file you should use write-output
Write-Output "$item : OFFLINE" | Out-File -Append PC_Offline.txt
write-host is really only for output to the console
1 Spice up
Ta mate - working like a charm.
@britv8