question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`$PSScriptRoot` and `$PSCommandPath` are not populated with `Start-Job -FilePath <filepath>`

See original GitHub issue

Steps 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mklement0commented, Mar 24, 2021

The explanation for the current behavior (which is definitely debatable) is implied by this line from the docs:

When you use this parameter, PowerShell converts the contents of the specified script file to a script block and runs the script block as a background job.

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.

0reactions
mklement0commented, Mar 28, 2021

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:

Start-Job { .\script.ps1 } | Receive-Job -Wait

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found