It's something unbelievable. This is a PowerShell code snippet in test.ps1 file:

Set-StrictMode -Version 2
mkdir c:\tmp\1  # same with 'md c:\tmp\1'

Start cmd.exe, navigate to folder with test.ps1 script and run it:

c:\tmp>powershell ".\test.ps1"

This produces the following error:

The variable '$_' cannot be retrieved because it has not been set.
At line:50 char:38
+         $steppablePipeline.Process($_ <<<< )
    + CategoryInfo          : InvalidOperation: (_:Token) [], ParentContainsEr
   rorRecordException
    + FullyQualifiedErrorId : VariableIsUndefined

Why?

It works when started from PowerShell console but not cmd.exe. I discovered this bug in much larger script. It was a WTF moment.

What is wrong with this simple script?

Edit
Report