powershellactive-directorypowershell-2.0windows-users

Check if user is service account


I try to decide with use of host command below if a given username is service account or not.

Get-ADUser $username -Properties PasswordNeverExpires |
  where { $_.PasswordNeverExpires -eq "true" } |
  where { $_.Enabled -eq "true"}

It should return only with one value, maybe with a True or False. How could I do this?


Solution

  • Cast the expression to a [bool] - if no user with those criteria exist it will be $false, otherwise $true:

    $SAExists = [bool](Get-ADUser -Filter {SAMAccountName -eq $username -and PasswordNeverExpires -eq $true -and Enabled -eq $true})