Microsoft Technical Takeoff: Windows and Microsoft Intune
Oct 24 2022 07:00 AM - Oct 27 2022 12:00 PM (PDT)

Bug in PowerShell version 5.1.17134.165 $Using: fails when used interactively in Start-Job

Contributor

The following script fails when used interactively (F8 in ISE or pasted in the console) on Windows 10 1803;

$FilePath = "C:\Temp\JobTest2.txt"
$Job = Start-Job -ScriptBlock {Out-File -InputObject $args -FilePath $Using:FilePath} -ArgumentList $FilePath 
$Job |Wait-Job |Receive-Job -Keep
Get-Content $FilePath

It works up to 1709 (PS 5.1.16299.492) and is working in 6.0.2 Core. In 1803 if the script is saved and called it will work.

I have also found a work around, if you add ";$null" after "$Using:FilePath" (it also works specifying other variables) e.g.

$Job = Start-Job -ScriptBlock {Out-File -InputObject $args -FilePath $Using:FilePath ;$null} -ArgumentList $FilePath

0 Replies