Table of Contents
Using Get-ADUser Cmdlet
Use the Get-ADUser cmdlet to get the password expiration date for one specific user in an Active Directory.
|
1 2 3 4 5 6 |
Get-ADUser -Identity Administrator –Properties "SamAccountName", "msDS-UserPasswordExpiryTimeComputed" | Select-Object -Property "SamAccountName", @{Name="PasswordExpiryDate";Expression={[DateTime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}} |
|
1 2 3 4 5 |
SamAccountName PasswordExpiryDate -------------- ------------------ Administrator 8/27/2023 4:35:46 PM |
The Get-ADUser cmdlet is used to get one or multiple users from an active directory. We used it with the -Identity parameter to only get the Administrator user with SamAccountName and msDS-UserPasswordExpiryTimeComputed properties. We specified these properties using the -Property parameter.
We piped the details (retrieved from the Get-ADUser cmdlet) to the Select-Object cmdlet to select the values of the SamAccountName and PasswordExipryDate properties. Where did the PasswordExpiryDate property come from while we retrieved msDS-UserPasswordExpiryTimeComputed using Get-ADUser?
We used a HashTable to define custom PowerShell object to create a calculated/custom property whose name would be PasswordExpiryDate and the value would be calculated using msDS-UserPasswordExpiryTimeComputed property.
While creating a calculated property, the expression computed the value of the PasswordExpiryDate property. It used the FromFileTime() method of the DateTime class to convert the msDS-UserPasswordExpiryTimeComputed to a DateTime object.
The reason for doing so was to make the output easier to understand. In HashTable, the semicolon was used as a separator. If you don’t want to use it, define Name and Expression on separate lines by hitting Enter from the keyboard.
Use the Get-ADUser cmdlet to get the password expiration date for all users in an Active Directory.
|
1 2 3 4 5 6 |
Get-ADUser -Filter {Enabled -eq $True -and PasswordNeverExpires -eq $False} –Properties "SamAccountName", "msDS-UserPasswordExpiryTimeComputed" | Select-Object -Property "SamAccountName", @{Name="PasswordExpiryDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}} |
|
1 2 3 4 5 |
SamAccountName PasswordExpiryDate -------------- ------------------ Administrator 8/27/2023 4:35:46 PM |
This example resembles the previous one but we got the expiration date for all the users in an active directory using the specified conditions. Here, we used the -Filter parameter to select only those uses whose Enabled and PasswordNeverExpires properties were equal to $True and $False, respectively. We used -eq to determine the equality while -and was a logical AND operator, which makes the conditional expression true if both conditions are true.
In our case, the conditions were only met for the Administrator user, so we got its password expiry date.
Use the following script if you want to export the sorted output to a CSV file.
|
1 2 3 4 5 6 7 |
Get-ADUser -Filter {Enabled -eq $True -and PasswordNeverExpires -eq $False} –Properties "SamAccountName", "msDS-UserPasswordExpiryTimeComputed" | Select-Object -Property "SamAccountName", @{Name="PasswordExpiryDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}| Sort-Object "PasswordExpiryDate" | Out-File C:\PasswordExpiryDates.csv |
|
1 2 3 4 5 |
SamAccountName PasswordExpiryDate -------------- ------------------ Administrator 8/27/2023 4:35:46 PM |
This code snippet is the same as the previous one with two additional cmdlets. We used the Sort-Object cmdlet to sort the received input based on the PasswordExpiryDate and piped it to the Out-File cmdlet, which wrote it into the specified file. In our case, it was PasswordExpiryDates.csv
The
Sort-Objectby default sorts in ascending order. Use the-Descendingparameter to sort in descending order.
Further reading:
Using net user Command
Use the net user command to get the password expiration date in the active directory. Usually, this command is used when you quickly want to retrieve the password expiry date for one particular user.
|
1 2 3 |
net user Administrator /domain | find "Password expires" |
|
1 2 3 |
Password expires 8/27/2023 4:35:46 PM |
Let’s take another example below.
|
1 2 3 |
net user john.powell /domain | find "Password expires" |
|
1 2 3 |
Password expires Never |
Do not forget to replace the Administrator and john.powell with your username.
That’s all about get password expiration date in PowerShell.