`$PSScriptRoot` and `$PSCommandPath` are not populated with `Start-Job -FilePath <filepath>`
See original GitHub issueSteps to reproduce
script.ps1:
echo $PSCommandPath
echo $PSScriptRoot
Run this in terminal:
Start-Job -FilePath .\script.ps1 | Receive-Job -Wait
Expected behavior
<path-to-script.ps1>
<path-to-scrpt.ps1-dir>
Actual behavior
(2 empty lines)
Environment data
PSVersion 7.2.0-preview.3
PSEdition Core
GitCommitId 7.2.0-preview.3
OS Microsoft Windows 10.0.19042
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
$PSScriptRoot is not populated when running a code block ...
I am trying load DLLs that are in the same directory as the PowerShell script and use $PSScriptRoot to get the location for...
Read more >Powershell $PSScriptRoot empty when using Invoke- ...
Therefore, $PSScriptRoot (the directory in which a script file resides) doesn't apply and returns the empty string.
Read more >start-job and $PSScriptRoot/$MyInvocation : r/PowerShell
The problem is that he/she wants the directory where the script is running while inside the job. This will often be different than...
Read more >about Automatic Variables - PowerShell
This variable is populated when you start PowerShell with the PSConsoleFile ... Contains the full path of the user's home directory.
Read more >PowerShell Start-Job WTF - mnaoumov.NET - WordPress.com
The term 'PSScriptRoot' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of th...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The explanation for the current behavior (which is definitely debatable) is implied by this line from the docs:
That is, the background job doesn’t execute the script file given, but its contents, as a script block - and such a script block has no information about where it ultimately came from.
While this behavior makes perfect sense in the context of remoting (with
Invoke-Command -ComputerName $someComputer -FilePath ...
), it arguably makes better sense to let a background job - which by definition runs on the same machine and can therefore access the specified file - execute the script file directly, so as not to lose invocation context.I don’t think security concerns are in the picture here, and my guess is that this was done more for the convenience of sharing an implementation with / behavioral symmetry with
Invoke-Command -FilePath
That said, @MatejKafka, the problem is easily worked around by using a script block instead:
Note that in Windows PowerShell this won’t necessarily work, because background jobs there don’t inherit the caller’s location, but in PowerShell Core that isn’t a problem anymore.