I am having some difficulties with the output of the Account Expiration Date from some users in our AD.
This is the code I am using:
Get-ADUser -Properties AccountExpirationDate
Problem is when I have a user in AD that has not set a expiration date it shows blank. I want that it shows 'Never Expires' because that is the case. When I check a user with expiration date it will show me the exact expiry date.
I also tried with if else statement, but no luck so far.
Thanks in advance.
Regards, Ralph
Test if the value is $null
:
$user = Get-ADUser $username -Properties AccountExpirationDate |Select SAMAccountName,@{Name='AccountExpiration'; Expression={if($null -eq $_.AccountExpirationDate){'Never Expires'}else{$_.AccountExpirationDate}}}
Another way could be to also ask for the LDAP accountExpires
property, which is a numeric value.
If that user property equals to 0 or 9223372036854775807, then the account never expires.
Get-ADUser -Properties AccountExpirationDate, accountExpires |
Select-Object Name, DistinguishedName,
@{Name = 'AccountExpirationDate'
Expression = {
if ($_.accountExpires -gt 0 -and $_.accountExpires -ne 9223372036854775807) { $_.AccountExpirationDate }
else { 'Never Expires' }
}}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.