Support
 
Phone Live Chat
 
Support
 
US: +1 888 720 9500
US: +1 800 443 6694
Intl: +1 925 924 9500
Aus: +1 800 631 268
UK: 0800 028 6590
CN: +86 400 660 8680

Direct Inward Dialing: +1 408 916 9393

 
 
 
 
 
Knowledge Base

How to return error codes in PowerShell scripts?

Returning error codes in PowerShell scripts allows you to communicate the success or failure of script execution to other scripts, programs, or users. By using the exit statement followed by a numeric value, you can set the error code to indicate different outcomes. Typically, an exit code of 0 indicates success, while non-zero values signify errors. This facilitates error handling, automation, and monitoring of script execution, enhancing the reliability and maintainability of your PowerShell automation solutions.

Error handling in PowerShell

Error handling in PowerShell scripts involves implementing mechanisms to gracefully manage and respond to errors that may occur during script execution. This includes using try-catch blocks, error action parameters, error variables such as $Error and $LASTEXITCODE, and the throw statement. By employing these techniques, PowerShell scripts can effectively detect, handle, and report errors, enhancing their reliability and usability.

Using $LASTEXITCODE for error code handling in PowerShell scripts

The $LASTEXITCODE automatic variable stores the exit code of the last command executed in your script. This can be helpful for capturing the exit code of external tools or commands you call within your script. A sample script is shown below.

Script

Write-host "Welcome to connection check"

$machine = Read-Host "Please enter your machine host/ip"

$pingResult = Test-Connection -ComputerName $machine -quiet

if ($pingResult -eq $true) {

Write-Host "Machine reachable '$machine'."

exit 0

} else {

Write-Warning "Failed to connect '$machine'."

exit -1

}

Output

How to return error codes in PowerShell scripts?

  • In the first execution, the user enters localhost as the machine host/IP. The script successfully verifies the connection, as indicated by the message Machine reachable 'localhost'. The $LASTEXITCODE variable is then checked, and its value is 0, which typically indicates successful execution without errors.
  • In the second execution, the user enters localhost-123 as the machine host/IP. However, the script fails to establish a connection, as indicated by the warning message Failed to connect 'localhost-123'. In this case, the $LASTEXITCODE variable is checked again, and its value is -1, which indicates that the script encountered an error during execution.

ADManager Plus simplifies scripting beyond PowerShell

In the realm of AD management, PowerShell scripting has been a crucial tool for automating repetitive tasks and streamlining administrative workflows. However, the complexity and syntax of PowerShell often pose challenges for IT administrators in their routine AD tasks.

This is where ADManager Plus steps in as a comprehensive AD management tool, surpassing PowerShell to simplify scripting and empower administrators with intuitive automation capabilities. Here are some benefits of choosing ADManager Plus as your alternative for PowerShell.

  • User-friendly interface: ADManager Plus offers a user-friendly, web-based interface, making it accessible to non-technical users who may not be proficient in scripting languages like PowerShell.
  • Prebuilt tools: ADManager Plus provides prebuilt tools and features for common AD management tasks, reducing the need for users to write complex scripts from scratch.
  • Automation workflows: With preconfigured automation workflows, ADManager Plus simplifies and streamlines management processes, saving time and effort.
  • Extensive reporting: ADManager Plus offers extensive reporting capabilities, allowing users to easily monitor and audit AD changes without writing custom scripts.

 

Select a language to translate the contents of this web page:

Need further assistance?

Fill this form, and we'll contact you rightaway.

Request Support

  •  
  • *
     
  • *
     
  • *
     
  • By submitting you agree to processing of personal data according to the Privacy Policy.

"Thank you for submitting your request.

Our technical support team will get in touch with you at the earliest."

ADManager Plus Trusted By

The one-stop solution to Active Directory Management and Reporting
Email Download Link email-download-top