Incorrect error code when you run a PowerShell script at a command prompt in Windows 7 or in Windows Server 2008 R2

Article translations Article translations
Article ID: 2552055 - View products that this article applies to.
Expand all | Collapse all

On This Page

SYMPTOMS

Consider the following scenario:
  • You run a PowerShell script at a command prompt on a computer that is running Windows 7 or Windows Server 2008 R2.
  • You use the exit statement in the PowerShell script to exit the script with a custom error code.
  • You run the echo %errorlevel% command at the command prompt to retrieve the error code.
In this situation, an incorrect error code is returned. Instead of the custom error code that is specified by the exit statement, the error code that is returned is zero.

CAUSE

This issue occurs because of an error in PowerShell 2.0.

RESOLUTION

Hotfix information

A supported hotfix is available from Microsoft. However, this hotfix is intended to correct only the problem that is described in this article. Apply this hotfix only to systems that are experiencing the problem described in this article. This hotfix might receive additional testing. Therefore, if you are not severely affected by this problem, we recommend that you wait for the next software update that contains this hotfix.

If the hotfix is available for download, there is a "Hotfix download available" section at the top of this Knowledge Base article. If this section does not appear, contact Microsoft Customer Service and Support to obtain the hotfix.

Note If additional issues occur or if any troubleshooting is required, you might have to create a separate service request. The usual support costs will apply to additional support questions and issues that do not qualify for this specific hotfix. For a complete list of Microsoft Customer Service and Support telephone numbers or to create a separate service request, visit the following Microsoft Web site:
http://support.microsoft.com/contactus/?ws=support
Note The "Hotfix download available" form displays the languages for which the hotfix is available. If you do not see your language, it is because a hotfix is not available for that language.

Prerequisites

To apply this hotfix, you must be running one of the following operating systems:
  • Windows 7
  • Windows 7 Service Pack 1 (SP1)
  • Windows Server 2008 R2
  • Windows Server 2008 R2 Service Pack 1 (SP1)
For more information about how to obtain a Windows 7 or Windows Server 2008 R2 service pack, click the following article number to view the article in the Microsoft Knowledge Base:
976932 Information about Service Pack 1 for Windows 7 and for Windows Server 2008 R2

Registry information

To use the hotfix in this package, you have to make any changes to the registry.

Restart requirement

You have to restart the computer after you apply this hotfix.

Hotfix replacement information

This hotfix does not replace a previously released hotfix.

File information

The global version of this hotfix installs files that have the attributes that are listed in the following tables. The dates and the times for these files are listed in Coordinated Universal Time (UTC). The dates and the times for these files on your local computer are displayed in your local time together with your current daylight saving time (DST) bias. Additionally, the dates and the times may change when you perform certain operations on the files.
Windows 7 and Windows Server 2008 R2 file information notes
  • The files that apply to a specific product, milestone (RTM, SPn), and service branch (LDR, GDR) can be identified by examining the file version numbers as shown in the following table:
    Collapse this tableExpand this table
    VersionProductMilestoneService branch
    6.1.760 0.21xxxWindows 7 and Windows Server 2008 R2RTMLDR
    6.1.760 1.21xxxWindows 7 and Windows Server 2008 R2SP1LDR
  • The MANIFEST files (.manifest) and the MUM files (.mum) that are installed for each environment are listed separately in the "Additional file information for Windows Server 2008 R2 and for Windows 7" section. MUM and MANIFEST files, and the associated security catalog (.cat) files, are extremely important to maintain the state of the updated components. The security catalog files, for which the attributes are not listed, are signed with a Microsoft digital signature.
For all supported x86-based versions of Windows 7
Collapse this tableExpand this table
File nameFile versionFile sizeDateTimePlatform
System.management.automation.dll6.1.7600.210623,010,56001-Oct-201104:46x86
System.management.automation.dll6.1.7601.218303,010,56001-Oct-201106:16x86
For all supported x64-based versions of Windows 7 and of Windows Server 2008 R2
Collapse this tableExpand this table
File nameFile versionFile sizeDateTimePlatform
System.management.automation.dll6.1.7600.210623,010,56001-Oct-201104:46x86
System.management.automation.dll6.1.7601.218303,010,56001-Oct-201106:16x86
For all supported IA-64-based versions of Windows Server 2008 R2
Collapse this tableExpand this table
File nameFile versionFile sizeDateTimePlatform
System.management.automation.dll6.1.7600.210623,010,56001-Oct-201104:46x86
System.management.automation.dll6.1.7601.218303,010,56001-Oct-201106:16x86

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

For more information about how to write scripts for Windows PowerShell, visit the following Microsoft website:
General information about how to write scripts for Windows PowerShell
For more information about software update terminology, click the following article number to view the article in the Microsoft Knowledge Base:
824684 Description of the standard terminology that is used to describe Microsoft software updates

Example scripts and output

The following is an example of a NoCmdletBinding.ps1 PowerShell script:
Param(
            [string]$ExitCode
)
 
Write-Host "Exiting script with $ExitCode"
exit $ExitCode
The following is an example of a CmdletBinding.ps1 PowerShell script:
[CmdletBinding()]
Param(
            [Parameter(Position=0,ValueFromPipeline=$true,Mandatory=$true)]
            [string]$ExitCode
)
 
Write-Host "Exiting script with $ExitCode"
exit $ExitCode
The following is an example of the output that is generated when you run a PowerShell script at a command prompt:
C:\>powershell -noprofile -file d:\NoCmdletBinding.ps1 5
Exiting script with 5
C:\>echo %errorlevel%
5
C:\>powershell -noprofile -file d:\CmdletBinding.ps1 3
Exiting script with 3
C:\>echo %errorlevel%
0
In this example, the return value is incorrect. The return value should be 3 instead of 0.

The following is an example of the output that is generated when you run a script in a PowerShell window:
PS C:\temp> .\NoCmdletBinding.ps1 5
Exiting script with 5
PS C:\temp> $lastexitcode
5
PS C:\temp> .\CmdletBinding.ps1 3
Exiting script with 3
PS C:\temp> $lastexitcode
3
In this example, the return value is correct.

Additional file information

Additional file information for Windows 7 and for Windows Server 2008 R2

Additional files for all supported x86-based versions of Windows 7
Collapse this tableExpand this table
File nameMsil_system.management.automation_31bf3856ad364e35_6.1.7600.21062_none_21d77a095ab58e92.manifest
File versionNot Applicable
File size1,637
Date (UTC)01-Oct-2011
Time (UTC)05:06
File nameMsil_system.management.automation_31bf3856ad364e35_6.1.7601.21830_none_23dc700357c53de2.manifest
File versionNot Applicable
File size1,637
Date (UTC)01-Oct-2011
Time (UTC)06:41
File nameX86_10c2b77e66ca0e2cfb55633728bae0b5_31bf3856ad364e35_6.1.7601.21830_none_0d0228efca681817.manifest
File versionNot Applicable
File size699
Date (UTC)01-Oct-2011
Time (UTC)10:14
File nameX86_55cf080d4fa611a14334fdd2f335fba2_31bf3856ad364e35_6.1.7600.21062_none_2dc9be46d0a4ef30.manifest
File versionNot Applicable
File size699
Date (UTC)01-Oct-2011
Time (UTC)10:14
Additional files for all supported x64-based versions of Windows 7 and of Windows Server 2008 R2
Collapse this tableExpand this table
File nameAmd64_10c2b77e66ca0e2cfb55633728bae0b5_31bf3856ad364e35_6.1.7601.21830_none_6920c47382c5894d.manifest
File versionNot Applicable
File size701
Date (UTC)01-Oct-2011
Time (UTC)10:14
File nameAmd64_55cf080d4fa611a14334fdd2f335fba2_31bf3856ad364e35_6.1.7600.21062_none_89e859ca89026066.manifest
File versionNot Applicable
File size701
Date (UTC)01-Oct-2011
Time (UTC)10:14
File nameMsil_system.management.automation_31bf3856ad364e35_6.1.7600.21062_none_21d77a095ab58e92.manifest
File versionNot Applicable
File size1,637
Date (UTC)01-Oct-2011
Time (UTC)05:06
File nameMsil_system.management.automation_31bf3856ad364e35_6.1.7601.21830_none_23dc700357c53de2.manifest
File versionNot Applicable
File size1,637
Date (UTC)01-Oct-2011
Time (UTC)06:41
Additional files for all supported IA-64-based versions of Windows Server 2008 R2
Collapse this tableExpand this table
File nameIa64_10c2b77e66ca0e2cfb55633728bae0b5_31bf3856ad364e35_6.1.7601.21830_none_0d03cce5ca662113.manifest
File versionNot Applicable
File size700
Date (UTC)01-Oct-2011
Time (UTC)10:14
File nameIa64_55cf080d4fa611a14334fdd2f335fba2_31bf3856ad364e35_6.1.7600.21062_none_2dcb623cd0a2f82c.manifest
File versionNot Applicable
File size700
Date (UTC)01-Oct-2011
Time (UTC)10:14
File nameMsil_system.management.automation_31bf3856ad364e35_6.1.7600.21062_none_21d77a095ab58e92.manifest
File versionNot Applicable
File size1,637
Date (UTC)01-Oct-2011
Time (UTC)05:06
File nameMsil_system.management.automation_31bf3856ad364e35_6.1.7601.21830_none_23dc700357c53de2.manifest
File versionNot Applicable
File size1,637
Date (UTC)01-Oct-2011
Time (UTC)06:41

Properties

Article ID: 2552055 - Last Review: November 14, 2011 - Revision: 2.0
APPLIES TO
  • Windows 7 Enterprise
  • Windows 7 Home Basic
  • Windows 7 Home Premium
  • Windows 7 Professional
  • Windows 7 Starter
  • Windows 7 Ultimate
  • Windows Server 2008 R2 Datacenter
  • Windows Server 2008 R2 Enterprise
  • Windows Server 2008 R2 for Itanium-Based Systems
  • Windows Server 2008 R2 Foundation
  • Windows Server 2008 R2 Standard
  • Windows Web Server 2008 R2
Keywords: 
kbautohotfix kbqfe kbhotfixserver kbfix kbsurveynew kbexpertiseadvanced KB2552055

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com