How to add PSComputerName to Last Logon User Script

Winston Willocks 1 Reputation point
2021-09-20T18:49:26.033+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,363 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. MotoX80 31,571 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 
    
    0 comments No comments

  2. Rich Matheisen 44,776 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.

    0 comments No comments

  3. Limitless Technology 39,351 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.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://learn.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,

    0 comments No comments

  4. Winston Willocks 1 Reputation point
    2021-09-22T00:04:50.307+00:00

    Thanks everyone for their, it really help

    0 comments No comments