# How to return Error codes on Scripts? ## Description Every command or script returns with the status of execution, which is referred as return status or exit codes. A successful command returns a 0 while an unsuccessful one returns a non-zero value that usually can be interpreted as an Error Code. The last command executed in the function or the script determines the exit status. This document provides steps on how to return the error codes on `.vb` scripts, Powershell scripts and batch files. ## Steps ### Exit codes for batch files Use the command **EXIT /B %ERRORLEVEL%** at the end of the batch file to return the error codes from the batch file. - **EXIT /B** at the end of the batch file will stop execution of a batch file. - Use **EXIT /B ** at the end of the batch file to return custom return codes. - Environment variable **%ERRORLEVEL%** contains the latest error level in the batch file, which is the latest error code from the last command executed. **Note:** Environment variables are a set of dynamic named values that can affect the way running processes behave on a computer. For example, an environment variable with a standard name can store the location that a particular computer system uses to store user profiles; this may vary from one computer system to another. In the batch file, it is always a good practice to use environment variables instead of constant values, since the same variable gets expanded to different values on different computers. **Example:** Batch file for copying a file to a folder: ```bat md "C:\manageengine" copy "\\sharename\foldername\samplefile.txt" "C:\manageengine" exit /b %ERRORLEVEL% ``` ### Exit codes for Powershell script Use the command `Exit $LASTEXITCODE` at the end of the Powershell script to return the error codes from the Powershell script. - **$LASTEXITCODE** holds the last error code in the Powershell script. It is in the form of boolean values, with 0 for success and 1 for failure. - `Exit ` will return custom return codes from the script. **Example:** Powershell script for copying a file to a folder: ```powershell $dest ="C:\test" New-Item $dest -type directory -force $source ="C:\samplefile.txt" Copy-Item $source $dest exit $LASTEXITCODE ``` ### Exit codes for VB script Use **wscript.quit Err.Number** at the end of the VB script, which will return the last error code from the script. - **Err.Number** holds the last error code in the script. - `wscript.quit ` will return custom return codes from the script. **Example:** VB script for copying a file to a folder: ```vbscript dim filesys set filesys = CreateObject("Scripting.FileSystemObject") If filesys.FileExists("C:\samplefile.txt") Then filesys.CopyFile "C:\samplefile.txt", "C:\manageengine" End If wscript.quit Err.Number ```