Wrap the cmdlet in a Try/Catch:
Try{
Get-ADUser -Identity xxxxx -ErrorAction STOP
# code goes here for a successful execution
}
Catch{
# code goes here for a failed attempt
}
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Good morning!
I'm very new to Powershell scripting so please give me your utmost support whenever possible. Thank you in advance.
Scenario
I have been trying to work on a condition based on the result of Get-ADUser -Identity <Input>. Whenever I use that statement for an none existence user account, it will always be the default error message. I need to have it return a certain value be it True/False, 1/0 or anything else so that I can use the result from that statement to perform my task.
Can anyone please advise?
Thank you once again and best regards.
Ronald
Wrap the cmdlet in a Try/Catch:
Try{
Get-ADUser -Identity xxxxx -ErrorAction STOP
# code goes here for a successful execution
}
Catch{
# code goes here for a failed attempt
}
Hi,
besides what @Rich Matheisen suggested you can always do the following:
if ( Get-ADUser -Identity <Input>) {
#True (1)
} else {
#False (0)
}
although I find the try and catch approach better.
=========================================================
Please don't forget to "Accept Answer" and upvote if the response helped you.
Stoyan