Thanks everyone for their, it really help
How to add PSComputerName to Last Logon User Script
I wrote the script below on how to get the last logon users from the registry. I would like to add PSComputerName, last logon time, format it and export to a CSV file, help would really be appreciated .
$computer = Get-Content -path 'C:\Users\Public\Computer\computers.txt'
Invoke-Command -ComputerName $computer -ScriptBlock {
$regpath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI'
$regvalue = Get-ItemProperty -Path $regpath
$regvalue.LastLoggedOnUser
$regvalue.LastLoggedOnDisplayName
}
4 answers
Sort by: Newest
-
-
Limitless Technology 39,376 Reputation points
2021-09-21T09:57:07.887+00:00 Hello,
The log-on user’s information is stored in the registry. You can check them out in the following location in the RegEditor app.
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI
Get-ItermProperty is a very useful PowerShell cmdlet that lets you retrieve information from your registry effectively.
$regpath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI'
$regvalue = Get-ItemProperty -Path $regpath
$regvalue.LastLoggedOnUser
$regvalue.LastLoggedOnDisplayNameTo get the information from a remote computer, wrap it up in a Invoke-Command cmdlet.
$computer = Read-Host 'Computer'
Invoke-Command -ComputerName $computer -ScriptBlock {
$regpath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI'
$regvalue = Get-ItemProperty -Path $regpath
$regvalue.LastLoggedOnUser
$regvalue.LastLoggedOnDisplayName
}Repeat the process if you want to get the last logged on user information from a group of computers.
Note that
in order to get the PowerShell scripts to work on remote computers, there are two prerequisites that need to be met.
Here's a good link as to what I did with the Select-Object cmdlet:
WinRM needs to be enabled on the remote computer
You need proper credentials to run the script on the remote computer.I hope this will help you further.
Regards,
-
Rich Matheisen 45,091 Reputation points
2021-09-20T21:40:12.137+00:00 See if something like this works for you:
[array]$computers = Get-Content -path 'C:\Users\Public\Computer\computers.txt' Invoke-Command -ComputerName $computers -ScriptBlock { $regpath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI' Get-ItemProperty -Path $regpath } | ForEach-Object{ [PSCustomObject]@{ ComputerName = $_.PSComputerName LastLoggedOnUser = $_.LastLoggedOnUser LastLoggedOnDisplayName = $_.LastLoggedOnDisplayName } }
I'm not sure if the PSComputerName property will be present if there's only on item in the $computers variable, that's why I explicitly cast the property an an array.
-
MotoX80 31,826 Reputation points
2021-09-20T21:28:51.14+00:00 Maybe something like this....
$computer = Get-Content -path 'c:\temp\Servers.txt' #'C:\Users\Public\Computer\computers.txt' $results = Invoke-Command -ComputerName $computer -ScriptBlock { $regpath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI' $regvalue = Get-ItemProperty -Path $regpath [PSCustomObject]@{ LastLoggedOn = $regvalue.LastLoggedOnUser DisplayName = $regvalue.LastLoggedOnDisplayName } } $results | Select-Object PSComputerName, LastLoggedOn, DisplayName | export-csv -Path C:\temp\result.csv