Get Account Expiry Date for the list of users - Powershell
The standard approach lets you to get the account expiry details for the entire ADuser and its also time consuming. Since it has to return the all the users with their expiry details.
Querying such a large database takes lot of time.
Below script will allow you to narrow down your search query.
Script:
#import AD module
Import-Module ActiveDirectory;
#save the list of users in text file at any given location, mention that path below
$userlist = Get-Content "C:\folder\listofuser.txt";
# $output variable will save the result in it
$Output = foreach($user in $userlist)
{
#Querying each user and returning their respective details by using Select
Get-ADUser -property * -ldapfilter "(samaccountname=$user)"| select sAMAccountName, Displayname,accountExpirationDate, Enabled,
Title
};
#Mention the filename and path below to save the output
$Output| Export-csv -path "c:\folder\result.csv"
How to Use:
1. Open notepad, copy all the users that you want to query for account expiration date.
2. Mention that path in the script
3. Mention the path for result of the script to return
4. Execute
Thanks guys,
Please comment down below if you have any query.
Have Nice day!
3 Comments
This is exactly what i need however when i test the script on three users i get an empty csv file
BalasHi Ninja, likewise I get empty oputput. What format does the text file need to be, please? Is it a list of users in a colomn with a carriage return between each, or maybe all just listed side-by-side seperated by commas? Can you give us an example of yopur .txt file for the users please?
BalasThanks for everything!!! :-)
Hi what to to I need an error message on the output file if incase a user ID has is not found or incorrectly entered.
Balas