How can I output Active Directory users with date yyyy-mm-dd and OU path?

1k Views Asked by At

How could I write a PowerShell script that will export all AD users by USER > LAST LOGIN DATE > PASSWORD CHANGE DATE > ROOT OU > SUB OU > SUB OU with all data separated by cells in a CSV file?

It would be nice to have the everything separated by cells with headers, date output with year first yyy-mm-dd, and OU path outputted with root OU first.

Preferred output

1

There are 1 best solutions below

3
RamaraoAdapa On

I tested in my environment.

You can use below PowerShell command

get-aduser -filter * -properties PasswordLastSet,CanonicalName | select Name,@{Name='Last Login Date';Expression={[DateTime]::FromFileTime($_.LastLogon).toString("yyyy-MM-dd HH:mm:ss")}},@{Name='Last Password Change Date';Expression={($_.PasswordLastSet).toString("yyyy-MM-dd HH:mm:ss")}},@{Name='OU';Expression={($_.CanonicalName)}} | export-csv 'path-to-csv-file'

enter image description here