Tech Community Live: Windows edition
Jun 05 2024, 07:30 AM - 11:30 AM (PDT)
Microsoft Tech Community

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

Brass 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