0

So basic a question but I cannot find an elegant, easy answer that I know exists. I want to know the result of this command so that I can modify what the script does afterwards:

Unlock-ADAccount -Identity user1234
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328

1 Answers1

2

You can evaluate the automatic variable $? to see if the command was successful or not:

Unlock-ADAccount -Identity 'user1234'
if ($?) {
  'Account unlocked.'
} else {
  'Unlocking failed.'
}

Or you can run the command with the parameter -PassThru, so it returns the user object, allowing you to check the lockout status:

Unlock-ADAccount -Identity 'user1234' -PassThru |
  Get-ADUser -Property 'LockedOut' |
  select -Expand 'LockedOut'

You need the additional Get-ADUser, because the default property set does not include the property LockedOut.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328