`$PSScriptRoot` is not populated when running a code block (via F8)

See original GitHub issue

System Details

  • Operating system name and version: Windows 10
  • VS Code version: 1.10.2
  • PowerShell extension version:
  • Output from $PSVersionTable:
PS C:\Source\neo4j-quick-demo> $pseditor.EditorServicesVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
0      11     0      0


PS C:\Source\neo4j-quick-demo>
PS C:\Source\neo4j-quick-demo> code --list-extensions --show-versions
PS C:\Source\neo4j-quick-demo>
PS C:\Source\neo4j-quick-demo> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.14393.953
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.953
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Issue Description

$PSScriptRoot is not populated when running a code block (via F8)

I am trying load DLLs that are in the same directory as the PowerShell script and use $PSScriptRoot to get the location for them.

When running the code in PowerShell it’s fine, but using VSCode, when running the code snippet via F8, the variable is not populated.

Repro

  • Open VSCode and create a PowerShell (.ps1) file
  • Add the following command Write-Host "$PSScriptRoot\abc"
  • Select the newly created line and Press F8 to execute the PowerShell in the integrated terminal

Expected result: <full working path>\abc

e.g. If the script I was editing was in C:\Source I would expect the result to be C:\Source\abc

Actual result: \abc

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:8
  • Comments:75 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
simonsabincommented, May 21, 2021

Sure that’s a fine working practice but there is many a time when you’ve got a script you’ve written that doesn’t need testing and doesn’t need splitting into loads of little chunks that you just want to run a little bit of it. Not being able to use psscriptroot is just annoying.

Simon Sabin


From: Justin Grote @.> Sent: Friday, May 21, 2021 12:53:37 AM To: PowerShell/vscode-powershell @.> Cc: Simon Sabin @.>; Mention @.> Subject: Re: [PowerShell/vscode-powershell] $PSScriptRoot is not populated when running a code block (via F8) (#633)

Or do your testing by writing it into a pester test and using the run test or debug test codelens rather than f5/f8

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/PowerShell/vscode-powershell/issues/633#issuecomment-845556082, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAJHM22UYXHISLVXTGHFSELTOWOIDANCNFSM4DFXJ4TQ.

2reactions
rkeithhillcommented, May 10, 2020

Yes, it is still an issue for F8. I’m not aware that this is an issue when debugging. In fact, I can’t repro this using F5.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PowerShell PSScriptRoot is null - Stack Overflow
I am using PS version 4. $val = Join-Path -Path $PSScriptRoot WebPlatformInstaller_amd64_en-US.msi. Error. Join-Path ...
Read more >
Path to this script, check ISE, VSCode, and running ... - Reddit
I noticed that $psscriptroot did not work in vscode when running a selection of code with F8. The above code works. What should...
Read more >
Automatic Variables - PowerShell - SS64.com
$PSScriptRoot will only be populated if the script has been saved to file and the entire file is executed, i.e. by pressing F5....
Read more >
PSScriptPad - PowerShell Pro Tools
Code Folding collapses blocks of PowerShell script that you wish to hide. ... It supports executing scripts, setting breakpoints, stepping through scripts ...
Read more >
PowerShellEditorServices | Keith Hill's Blog - WordPress.com
In the previous version of the extension the debugger would start using the install directory of Visual Studio Code as the working dir....
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