S.Mahmood
S.Mahmood

Reputation: 151

Get email addresses of users through powershell into CSV file

I have a PowerShell script (big thanks to Santiago Squarzon) that pulls off users who have been inactive for past 6 months.

Currently work fine and outputs:

1. Username 
2. Name
3. DistinguishedName
4. LastLogon

This is part of the code:

$logons.Values | ForEach-Object {
    [PSCustomObject]@{
        Name              = $_.Name
        SamAccountName    = $_.SamAccountName
        DistinguishedName = $_.DistinguishedName
        lastLogon         = [datetime]::FromFileTimeUtc($_.lastLogon).ToString('u')
    }
} | Export-CSV "C:\Users\Me\Desktop\InactiveUsers.CSV" -NoTypeInformation

I am also trying to pull off users email address. I have tried to add the following code but the EmailAddress field is blank in my CSV:

EmailAddress      = $user.EmailAddress 
EmailAddress      = $_.EmailAddress
EmailAddress      = $_.mail
EmailAddress      = $user.mail

None of the above work.

Upvotes: 0

Views: 342

Answers (0)

Related Questions