Powershell to output Last User Logon to specific XP machines

Need to create a powershell script to output to CSV last logged on user with a timestamp and to supply the machines operating system.

Heres what I have so far:

import-module ActiveDirectory
Get-ADComputer -Filter * -Property * | Select-Object Name,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion,sAMAccountName,@{N='LastLogon'; E={[DateTime]::FromFileTime($_.LastLogon)}} | Export-CSV c:\temp\AllWindows.csv -NoTypeInformation -Encoding UTF8
Who is Participating?
cantorisConnect With a Mentor Commented:
The problem is an AD computer account doesn't have an attribute for last logged on username.

You could use a WMI call to the PC to get the current user at the console of the machine.  The extra column would be this:

@{N='Current User';E={(Get-WmiObject -ComputerName ($_.DNSHostName) -Class Win32_ComputerSystem -EA SilentlyContinue) | Select-Object -ExpandProperty UserName}}

It's not ideal as you'll get a delay while each is contacted - esp if you have to wait for a timeout.
Why not make a simple bat login for user GPO?

That will report upon login and a logout will record the logout.
adrochoaAuthor Commented:
The problem I'm having is that not everyone restarts or log on at start of business. That mentioned process might take a whole year to report on 3000+ users.

The script mentioned above will get me All machines together with their last time logged on, but I still am missing what user logged in to it.
You could query the AD for user account and their last logon date.
You could scour the event log using the tool from aclocal

If you aggregate/forward logs from systems, you can find the information you want.

you can use tasklist /v or powershell wmi tie in to see what processes are running on each system with which username.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.