Avatar of gd6627
gd6627
Flag for United States of America asked on

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
PowershellActive Directory

Avatar of undefined
Last Comment
oBdA

8/22/2022 - Mon
oBdA

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

footech

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

gd6627

ASKER
When I run the script I get no Output
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
footech

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.
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.