PS script to get Windows 10 clients name and win version, and also get the last logged in SamAccountName on clients and export all into excell.xlmx document

Hi,

I hope someone inhere can help me designing af powershell script which does the following:

 

1) - Find all windows 10 clients in AD, and get thier windows version.

Then also get the either SamAccountName OR last SamAccountName logged in on the found windows 10 clients!

2) - Export Last login "SamAccountName", "OperationSystem" and "OperationSystemVersion" to an excell document.

 

I have the following code sofar:

 

Import-Module ActiveDirectory

Get-ADComputer -Filter * -Property * | Select-Object Name,OperatingSystem,OperatingSystemVersion | Export-CSV C:\AllWindows1.csv -NoTypeInformation -Encoding UTF8

 

But i need this to fetch the logged in/last login of the SamAccountName also on the windows 10 clients in the AD, and also export this to an excell.xlmx, if possible.

Can anyone help me?

Best regards

  • Hi, i have tried to work out a script myself, where i get some of the info needed.
    But i need some extra code help now.

    Current script:

    Import-Module ActiveDirectory
    Get-ADComputer -Filter * -Property * | Select-Object Name,OperatingSystem,OperatingSystemVersion | Export-CSV C:\AllWindows1.csv -NoTypeInformation -Encoding UTF8

    I would really like the following to be added to the script:

    1) Only get windows 10 clients, with the Notlike command?.
    2) Get the *Last logged on* on the found clientlist also, and display this in the Export also.

    Can anyone help me??
  • In reply to brdpe:

    Firstly, you can simplify this down to this:

    ((Get-ADComputer -Filter * -Properties *) | Select Name,LastLogonDate,OperatingSystem) -notmatch 'server' | ConvertTo-Csv

    As for getting the 'Last Logged On' user per machine you hit wish this query. That is not in AD, but in the Security Event log of each machine.

    Which means for every hit of a machine you have to ask for the most recent logged on event.
    Something like...

    Get-WinEvent -Computer $_.Name -FilterHashtable @{Logname='Security';ID=4672} -MaxEvents 1 | select @{N='User';E={$_.Properties[1].Value}}

    ... and of course add that as a property in your query for output.

    For example, If I wanted to do something like what you ask and was not worried about the whole OS filtering, I could do something like...

    (Get-ADComputer -Filter * -Properties * )`
    | % {$_.Name,$_.LastLogonDate,$_.OperatingSystem
    Get-WinEvent -Computer $_.Name -FilterHashtable @{Logname='Security';ID=4672} -MaxEvents 1 `
    | select @{N='User';E={$_.Properties[1].Value}}
    }

    This give you a list view of all the system in AD and the output you are after.

    What is your goal here?
    Are you really after log on for a particular machine or to the domain regardless of machine?
    If your users are allowed to take machines home, then the log on for local use just does not seem to have any real value vs logon to domain from that machine.

    Yet, I am sure you have your reasons.