question

WinstonWillocks-9097 avatar image
0 Votes"
WinstonWillocks-9097 asked WinstonWillocks-9097 answered

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
    
     }
windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered

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 
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

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.LastLoggedOnDisplayName

To 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:

https://docs.microsoft.com/en-us/previous-versions/technet-magazine/hh750381(v=msdn.10)?redirectedfrom=MSDN

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,

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WinstonWillocks-9097 avatar image
0 Votes"
WinstonWillocks-9097 answered

Thanks everyone for their, it really help

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.