Jeffrey
Jeffrey

Reputation: 2159

Get-ADUser - manipulating columns

My powershell script:

  $MaxPwdAge   = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Days
  $expiredDate = (Get-Date).addDays(-$MaxPwdAge)

  $users = Get-ADUser -Filter {(Enabled -eq $true)} -Properties PasswordNeverExpires, PasswordLastSet `
  | select samaccountname, PasswordLastSet, @{name = "DaysUntilExpired"; Expression = {$_.PasswordLastSet - $expiredDate | select -ExpandProperty Days}}, PasswordNeverExpires `
  | Sort-Object PasswordNeverExpires, PasswordLastSet

  $users | Format-Table | Out-String|% {Write-Host $_}
  Write-Host -NoNewLine "Press any key to return to the Main Menu..."
  $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Objective: I want to get rid of the column PasswordNeverExpires, and instead, in column DaysUntilExpired, I want it to say 'Password never expires' for the users that have Password never expires set.

Im still learning powershell, can someone advise me on how I could do this?

Upvotes: 0

Views: 321

Answers (1)

notjustme
notjustme

Reputation: 2494

I believe something like this would do the trick.

$MaxPwdAge   = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Days
$expiredDate = (Get-Date).addDays(-$MaxPwdAge)

$users = Get-ADUser -Filter {(Enabled -eq $true)} -Properties PasswordNeverExpires, PasswordLastSet `
    | select samaccountname, PasswordLastSet, @{name = "DaysUntilExpired"; Expression = {
        if($_.PasswordNeverExpires -eq $true) { "Password Never Expires" } else { $_.PasswordLastSet - $expiredDate | select -ExpandProperty Days}}
    } | Sort-Object PasswordLastSet

$users | Format-Table | Out-String|% {Write-Host $_}
Write-Host -NoNewLine "Press any key to return to the Main Menu..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Upvotes: 2

Related Questions