My snippet is something like this:
$msg=Remove-Item -Recurse -Force C:\\users\\bkp 2>&1
if ($LASTEXITCODE -eq 1)
{
\"Encountered error during Deleting
You can use the $?
automatic variable to determine the result of the last command. If you need access to the actual error, you can use the $Error
automatic variable. The first item in the array is the last error thrown:
Remove-Item -Recurse -Force C:\users\bkp 2>&1
if( -not $? )
{
$msg = $Error[0].Exception.Message
"Encountered error during Deleting the Folder. Error Message is $msg. Please check." >> $LogFile
exit
}