Powershell command to the Get Account expiration date, Extract user list which expire in a week time

By | August 12, 2018

Normally account expire will be configured for contract users for a specific dates, if they contract get extended then same needs to be updated on there Active Directory user accounts to avoid account get expired, to track this quite difficult and recently our first level team asked a script to give the list of users going to expire in next 7 days, so that they can check proactively and fix the issue before it affect the users, let me share couple of one line Powershell script/command

Also Read: PowerShell command to extract Group policy result for a list of Servers

Command to get list of Active Directory users that will expire in next one week (in a specific OU)

Search-ADAccount -AccountExpiring -SearchBase "CN=Users,DC=Windowstricks,DC=in" -UsersOnly -TimeSpan 07.00:00:00 | Sort-Object -Descending AccountExpirationDate

You can save the result to output file

Search-ADAccount -AccountExpiring -SearchBase "CN=Users,DC=Windowstricks,DC=in" -UsersOnly -TimeSpan 07.00:00:00 | Sort-Object -Descending AccountExpirationDate >>c:\temp\output.txt

You can change the OU details, number days you want to check and output file name and location

Also Read: Enable Powershell Remoting on Windows server 2008 R2 and 2012

Command to Get all the account and there Account expiry date

Search-ADAccount -AccountExpiring -UsersOnly | Sort-Object -Descending AccountExpirationDate
This will show all the user object with expire date, you can all get for in a specific OU by adding -SearchBase
Search-ADAccount -AccountExpiring -SearchBase "CN=Users,DC=Windowstricks,DC=in" -UsersOnly | Sort-Object -Descending AccountExpirationDate

Also Read: Interesting oneline script written using windows command

Also Read: Active Directory 2016 New Features

 

Leave a Reply

Your email address will not be published. Required fields are marked *