Modifying a script

gd6627
gd6627 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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

Top Expert 2014

Commented:
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

Author

Commented:
When I run the script I get no Output
Top Expert 2014

Commented:
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.
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial