ThreadJobs has access to the same environment as it was started in.
But normally, PowerShell will respond with a syntax error when trying to change variables from the parent level.
The documentation MS Learn - about_Thread_Jobs has som insights but nothing I could find useful.
The example below illustrates the issue when trying to use plain PowerShell variables.
[Array]$Numbers = @()
foreach ($i in 0..11) {
$Jobs = Start-ThreadJob {
$Using:Numbers += $using:i
}
}
$Jobs | Wait-Job | Remove-Job
$Numbers
ParserError:
Line |
6 | $Using:Numbers += $using:i
| ~~~~~~~~~~~~~~~
| The assignment expression is not valid. The input to an assignment operator must
| be an object that is able to accept assignments, such as a variable or a property.