We help IT Professionals succeed at work.

Modifying a script

gd6627
gd6627 asked
on
103 Views
1 Endorsement
Last Modified: 2018-10-02
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

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable 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

CERTIFIED EXPERT
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
CERTIFIED EXPERT
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.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions