Workflow, Get-lastlogged on User foreach Computer in the company

This topic contains 2 replies, has 2 voices, and was last updated by  Luca Wirths 6 months, 2 weeks ago.

  • Author
  • #78923

    Luca Wirths

    Hi everyone

    I am a newbie in powershell, so my code is not perfect.

    I am creating a script where I get the last logged on User on every computer in the company. It works, but it is slow. I saw the Worflow cmdlet and tried with this one, but it only returns 8 PCs and not all. Does someone have an idea?


    function Get-LastLoggedOnUser($computerName)
        Get-WmiObject Win32_NetworkLoginProfile -Computer $computerName |
        Sort -Descending LastLogon |
        Select * -First 1 |
        ? {$_.LastLogon -match "(\d{14})"} |
            % {
                New-Object PSObject -Property @{
                    computername = $computerName
    workflow Get-LastLoggedUseronPC
        $userList = @()
        foreach -parallel ($computer in $computerList)
            if (-not (Test-Connection -ComputerName $computer.DNSHostName -Quiet -count 1)){}
                $WORKFLOW:userList += Get-LastLoggedOnUser -computerName $computer.Name
    $computerList = Get-ADComputer -Filter{name -like "*"}| where {$ -le 8}
    PS C:\WINDOWS\system32>$userList
    computername Name
    ------------ ----
    PC100001     PC100001\MyUser
    PC100002     PC100002\otherUser
    PC100003     PC100003\anotherUser
    .            .
    .            .
    .            .
    PC100008     PC100008\testuser
  • #78928

    edwin arlington

    You can try this command:

    $MachineList = Get-Content -Path c:\ListOfMachines.txt; # One system name per line
    foreach ($Machine in $MachineList){
    ($Machine + ": " + @(Get-WmiObject -ComputerName $Machine -Namespace root\cimv2 -Class Win32_ComputerSystem)[0].UserName);

    Here are related articles for help.

  • #78931

    Luca Wirths

    Sorry, I was unclear about my result I want to achieve.
    I would like to get the last Computer where a User was logged on. The time when it happend is clear.
    So my plan is to get all PC last logged on user and then select the PC where the specific user was logged on.

You must be logged in to reply to this topic.