PowerShell return value, exit code, or ErrorLevel equivalent

Here is how you can verify whether an external command in PowerShell was executed successfully or not by its errorlevel. Simply by verifying PowerShell’s return value, or exit code…

Powershell $? operator

The PowerShell operator $? contains True if the last operation succeeded and False otherwise.

# source:
# http://blogs.msdn.com/b/powershell/archive/2006/09/15/errorlevel-equivalent.aspx
if( $? ) {
	# True, last operation succeeded

if( !$? ) {
	# Not True, last operation failed

To illustrate PowerShell’s $? usage, have a look at the following DISM Cleanup-Image command. In my Windows Server disk cleanup using DISM blogpost, I’ve shown you how to clean up your Windows Server WinSxs folder with DISM.

Those commands are easily wrapped into a PowerShell script, and here it is:

$os_version = [System.Environment]::OSVersion.Version

# The above returns 6.3.9600.0 for Server 2012 R2 or 
# 10.0.14393.0 for Server 2016. Server 2012 matches 
# 6.2.9200.0, and so on.
# See https://msdn.microsoft.com/nl-nl/library/windows/desktop/ms724832(v=vs.85).aspx
# for more information about Windows Server versions.

$cleanup = $false

# Always be careful comparing strings and integers!
if( $os_version -ge ( New-Object System.Version "10.0" )) {
	# $os_version is greater than, or equal to "10.0", so 
	    # this is Windows Server 2016.

if( $os_version -ge ( New-Object System.Version "6.3" ) -And $os_version -le ( New-Object System.Version "10.0" )) {
	# $os_version is greater than 6.3 and smaller than 10.0,
	# therefore this must be Windows Server 2012 R2

	&dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase | Write-Output
	if( $? ) {
		# dism cleanup-image was successful, set variable to True
		$cleanup = $true
if ( $cleanup ) {
	# Dism.exe was executed
	Write-Host "[*] System going down for reboot in 3 seconds!"
	&shutdown /r /f /t 3
} else {
	# an error occurred
	Write-Host "[*] Something went wrong with DISM and Cleanup-Image, `
		please perform the actions by hand."

How can I stop PowerShell errors from being displayed in a script?

Suppress error messages in PowerShell like a pro :) If you don’t want to display PowerShell errors completely, you can wrap your PowerShell commands in a Try{} / Catch{} block. For example:

Get-Website | % {
	$sitename = $_.name;
		$handlers = Get-WebConfiguration /system.webServer/handlers/add -Location $sitename
		If( $handlers.scriptProcessor -like "x:\php73\php-cgi.exe*" ) {
			write-output "$sitename uses PHP 7.3"
			&appcmd.exe recycle apppool $sitename
			# Restart-WebAppPool $sitename
	catch {}

This checks the registred handler to see if scriptProcessor contains x:\php73\php-cgi.exe*. If so, recycle that website’s application pool (assuming the website and apppool names are the same).

By using a Try{} / Catch{} block, you don’t see the PowerShell errors like:

Get-WebConfiguration : Filename: \?\z:\sites\www\example.com\www\web.config
 Line number: 14
 Error: There is a duplicate 'system.web.extensions/scripting/scriptResourceHandler' section defined
 At line:3 char:13
 $handlers = Get-WebConfiguration /system.webServer/handlers/add -Loca …
 ~~~~~~~~~~~~~ CategoryInfo          : NotSpecified: (:) [Get-WebConfiguration], COMException
 FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.IIs.PowerShell.Provider.GetConfigu


The PowerShell $LASTEXITCODE should be 0, since $LASTEXITCODE contains the exit code of the last Win32 executable execution.

$LASTEXITCODE the equivalent to cmd.exe %ERRORLEVEL%, and you can use it as follows in your PowerShell scripts:

&dism.exe /Online /Cleanup-Image /StartComponentCleanup /ResetBase
if( $LASTEXITCODE -eq 0 ) {
	Write-Output "Command executed successfully"
	# do something, like `Restart-Computer -Force`
} else {
	Write-Output "Last command failed"

Hi! Join the discussion, leave a reply!

Loading time: 101 queries, 0.223 seconds using 13595888 bytes memory. Peak memory usage: 13954760 bytes.