Below is the code I\'m using to find users created date and last logon date which is then written to a .csv file.
$users=Get-ADuser -SearchBase \"OU=testou1,dc=U
I would create an an array of objects with name and value properties. That will export nicely to CSV.
Bear in mind I'm writing this on a phone, I'll check it in my ISE later, but I've amended your code below:
$users=Get-ADuser -SearchBase "OU=testou1,dc=US,dc=ITOPS,dc=COM" -Filter * -properties samaccountname,lastlogondate,Created
$array = @() #declare the array outside the loop
$forloop = foreach($user in $users) {
$arrayrow = New-Object System.Object
$arrayrow | Add-Member -type NoteProperty -name User -value $user.samaccountname
$arrayrow | Add-Member -type NoteProperty -name Created -value $user.created
$arrayrow | Add-Member -type NoteProperty -name LastLogonDate -value $user.lastlogondate
$array += $arrayrow
}
$array | export-csv -notype c:\bin\exporting.csv