Link to home
Start Free TrialLog in
Avatar of gd6627
gd6627Flag 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
Avatar of oBdA
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

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

Avatar of gd6627

ASKER

When I run the script I get no Output
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
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial