How to handle powershell script execution error

I have created a script to remove AD user from DC , script execute all the time even user exists or does not exists.

Can we handle exception for action status if user does not exist comes as status failed or some message or should not be executed at all??

action parameter query "Identity" with description "Please enter the AD User account" with default value "" // Enter your action script here

// Disable wow64 redirection on x64 OSes
action uses wow64 redirection {not x64 of operating system}
delete __createfile

// CREATEFILE
createfile until END_OF_FILE

Remove-ADUser -Identity {parameter “Identity”} -Confirm:$false
END_OF_FILE

delete powershell.ps1
move __createfile powershell.ps1
waithidden { pathname of file ((it as string) of value “Path” of key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell” of native registry) } -ExecutionPolicy Bypass -File powershell.ps1

You would need to expand your powershell to include tests to account for anticipated exceptions. For example, query AD to see if the user exists, if not, log it and skip the ‘remove’ function. You would also want to verbosely log the output of your powershell execution to understand if one of those exceptions exists.

action parameter query "ad_group" with description "Please enter the AD group name" with default value "" action parameter query "ad_ou" with description "Please enter the AD OU name" with default value "" // Enter your action script here

// Disable wow64 redirection on x64 OSes
action uses wow64 redirection {not x64 of operating system}

// CREATEFILE
createfile until END_OF_FILE

NEW-ADGroup –name {parameter “ad_group”} –groupscope Global –path “OU={parameter “ad_ou”},DC=IAUTOMATE,DC=local”
$? > C:\temp\output.txt
$Error > c:\temp\error.txt

END_OF_FILE

delete powershell.ps1
move __createfile powershell.ps1

waithidden { pathname of file ((it as string) of value “Path” of key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell” of native registry) } -ExecutionPolicy Bypass -File powershell.ps1

parameter “file_content”="{lines of file “C:\temp\output.txt”}“
parameter “file_status”=”{lines of file “c:\temp\error.txt”}"

if { parameter “file_content” contains “False”}
continue if {parameter “file_status” contains “group already exists”}
endif