$MyInvocation.MyCommand object is set to null when you run the script by using PowerShell 3.0 in Windows 8 or in Windows Server 2012

Applies to: Windows 8Windows 8 EnterpriseWindows 8 Pro

Symptoms


Consider the following scenario:
  • You have some custom PowerShell scripts or modules that contain a reference to the built-in $MyInvocation instance.
  • You execute the scripts or modules on a Windows 8-based or Windows Server 2012-based computer by using Windows PowerShell 3.0.
  • Some objects are passed through a pipe.
In this scenario, the value of the $MyInvocation.MyCommand object is set to null.

For example, the issue occurs when you run the following script:
$Line= Get-Content $file
$Line | foreach-Object {
write-host "** MYCOMMAND [2] IS NULL?: " ($MyInvocation.MyCommand -eq $null) " --->" $_
}
Note This issue does not occur when you run the script by using Windows PowerShell 2.0.

Resolution


To resolve this issue, install update rollup 2855336, or install the hotfix that is described in this article. This hotfix is also available at Microsoft Update Catalog.

Update information

For more information about how to obtain update rollup 2855336, click the following article number to view the article in the Microsoft Knowledge Base: 
2855336 Windows RT, Windows 8, and Windows Server 2012 update rollup: July 2013



Workaround


To work around this issue, do not use a pipe to pass the objects. For example, you can change the example script to the following:
$Line= Get-Content $file
foreach( $L in $Line){
write-host "** MYCOMMAND [2] IS NULL?: " ($MyInvocation.MyCommand -eq $null) " --->"$L
}

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 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