Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you reset the PowerShell runspace within Visual Studio Code

I'm currently using version 0.10.0 of the PowerShell extension within Visual Studio Code. It used to be that the run space was reset between each run of a script so if there were changes made to a module, the updated module was imported into the script.

However, for some reason, the PowerShell run space is no longer being reset so a change in a module is not picked up during the next run.

Is there a setting within the PowerShell extension that will ensure the run space is reset between each run of a script?

like image 739
gerard Avatar asked Mar 16 '17 22:03

gerard


People also ask

How do I debug PowerShell in Vscode?

Now, switch back to your PowerShell file in the editor window and set breakpoints where you need them. Choose PowerShell Launch Current File option in the Debug menu above, and click on the green triangle to run the script in the debugger mode. The debugger will stop at the first breakpoint it encounters.

How do I create a PowerShell script using Visual Studio code?

PowerShell Extension Once you install the extension, you can create a new PowerShell script by pressing Ctrl+N and then saving it as a PS1 file using Ctrl+S . Once it is saved as a PS1, VS Code will identify the file as a PowerShell script. From there, you can execute the PowerShell script by press F5 .

What is PowerShell in Visual Studio code?

PowerShell in Visual Studio Code. PowerShell is a task-based command-line shell and scripting language built on . NET, which provides a powerful toolset for administrators on any platform.


2 Answers

The setting powershell.debugging.createTemporaryIntegratedConsole which is now available with in the PowerShell plugin for Visual Studio Code solved my problem.

The setting is defaulted to false so it must be enabled before it will work.

When it is set to true the PowerShell session is reset each time you try to debug through a script. This means that:

  • Constant variables can be reset
  • Updates to PowerShell modules will be pulled into the session

It can be enabled by adding the following line to the user settings JSON file

"powershell.debugging.createTemporaryIntegratedConsole": true

Or enable it in the Preferences under File -> Preferences -> Settings and search for Create Temporary Integrated Console

like image 171
gerard Avatar answered Oct 19 '22 02:10

gerard


I still dont have a good answer for this, but this is what I did to mitigate the problem during development.

I noticed that VS Code was caching the variables between each run, and that it effectively ran the script as if it was a REPL.

Due to the nature of PowerShell scopes, you can wrap your code in an Invoke-Command -ScriptBlock {} and as variables are not accessible outside of their script block, you can re-run the code without worrying about leaving objects in vs code.

like image 26
ConstantineK Avatar answered Oct 19 '22 00:10

ConstantineK