Modifying a script

I need to add email address to this script how can I make it where it returns the email in the select -object  when it sends it to the csv file


import-module activedirectory
 $domain = “domain.net”
$DaysInactive = 120
 $time = (Get-Date).Adddays(-($DaysInactive))

 Get-ADUser -Filter {LastLogonTimeStamp -lt $time -and enabled -eq $true} -Properties LastLogonTimeStamp |

 select-object samaccountname,Name,@{Name=”LAST Logon Time”; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} | export-csv c:\OLD_User.csv -notypeinformation
gd6627Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
The mail attribute is not included in thew default set of attributes returned by Get-ADUser, so you have to add it
Import-Module ActiveDirectory
$domain = "domain.net"
$DaysInactive = 120
$time = (Get-Date).Adddays(-($DaysInactive))
Get-ADUser -Filter "(LastLogonTimeStamp -lt '$($time)') -and (enabled -eq 'True')" -Properties LastLogonTimeStamp, Mail |
	Select-Object -Property SamAccountName, Name, Mail, @{Name=”LAST Logon Time”; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} |
	Export-Csv -NoTypeInformation -Path C:\OLD_User.csv

Open in new window

1
footechCommented:
How about just using the Mail attribute?
 Get-ADUser -Filter {LastLogonTimeStamp -lt $time -and enabled -eq $true} -Properties LastLogonTimeStamp,Mail |
 select-object samaccountname,Name,@{Name=”LAST Logon Time”; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}},mail | export-csv c:\OLD_User.csv -notypeinformation

Open in new window

1
gd6627Author Commented:
When I run the script I get no Output
0
footechCommented:
Which script?
The only change I made to yours was to add the Mail attribute, so if your command was returning items before it still will, unless there's something wrong outside of the script.
1
oBdACommented:
My bad - in reformatting the script, I changed the filter to the "officially" supported string, but the cmdlet does something with the filter and date that I can't reproduce with the string ...
Import-Module ActiveDirectory
$domain = "domain.net"
$DaysInactive = 120
$time = (Get-Date).Adddays(-($DaysInactive))
Get-ADUser -Filter {(lastLogonTimeStamp -lt $time) -and (enabled -eq $true)} -Properties LastLogonTimeStamp, Mail |
	Select-Object -Property SamAccountName, Name, Mail, @{Name=”LAST Logon Time”; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} |
	Export-Csv -NoTypeInformation -Path C:\OLD_User.csv

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.