2

Let's say I've been using Powershell ISE for a while and my environment space is starting to get dirty, and I need to restart the interactive shell... I don't want to close my editor and reopen it. How to restart powershell ISE interactive shell to clear all variables without closing and reopening the Powershell ISE?

enter image description here

mklement0
  • 382,024
  • 64
  • 607
  • 775
Bimo
  • 5,987
  • 2
  • 39
  • 61

3 Answers3

6

First, the obligatory notice:


ISE:

Colin's helpful answer is a pragmatic solution: open a new tab and close the old one.

However, that new session invariably retains the environment variables of the old one, because the ISE hosts the PowerShell SDK in-process rather than running powershell.exe as a child process.

To restart a session in the current tab you would therefore have to instruct the hosted System.Management.Automation.PowerShell instance to discard its current runspace and create a new one - and I'm not aware of a way to do this.

Even if it were possible, however, the environment variables - which exist at the level of the process that runs the ISE - would be retained.


Visual Studio Code:

It is possible to start a new session in the current tab and to do so without inheriting the old session's environment variables:

While the integrated terminal running is running the PowerShell Integrated Console, which the PowerShell extension comes with - which is the equivalent of the console pane in the ISE - you can kill the current instance by clicking the trash-can icon in the toolbar of the terminal panel as shown below.

After doing so, you're prompted for starting a new session, which runs in a new powershell.exe / pwsh child process.

kill the running shell in VS Code

Alternatively - and preferably - you can configure the PowerShell extension to automatically start a new session whenever you start a new debugging session, as zett42 points out:

  • Either: Open the Settings (Ctrl-,) view, search for powershell temporary and turn on the PowerShell > Debugging: Create Temporary Integrated Console setting.

  • Or: Add "powershell.debugging.createTemporaryIntegratedConsole": true directly to your settings.json file.

This automatically starts a new, temporary PowerShell Integrated Console for each debugging session, which remains open until you start the next debugging session, at which point a new temporary console simply replaces the old one.

  • Curiously, as of extension version 2022.11.0, you cannot exit out of a PowerShell Integrated Console, but you can use the trash-can icon or Stop-Process -Id $PID to kill it, if needed, which in the case of a temporary console will (commendably) not prompt you to restart it; instead, the next debugging session will create a new, temporary console on demand.

This configuration avoids a major pitfall that afflicts the ISE invariably (and may in part be what prompted the question) as well as the PowerShell extension's default configuration:

  • There, the code runs dot-sourced, i.e. directly in the top-level scope of the same session, so that the state left behind by earlier debugging runs can interfere with subsequent debugging runs; for instance, create a script with content (++$i) and run it repeatedly - you'll see that $i increments every time, across runs.

  • Starting a new session for every debugging run avoids this problem.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • @BillMoore: Fair point; they actually did implement an obsolescence warning of sorts for the Windows PowerShell _console_, where you now get a suggestion to try PowerShell [Core] instead on startup. If you feel inspired, you can suggest implementing something similar for the ISE [here](https://windowsserver.uservoice.com/forums/301869-powershell), though I wouldn't hold my breath. – mklement0 Dec 04 '20 at 15:48
  • 1
    Thanks for the info! Works great and will save me a bunch of time and frustration. The delay as the new terminal is opening is no worse than the compilation step when debugging in Visual Studio with other languages. – Jim D Dec 10 '22 at 15:20
  • Glad to hear it, @JimD. – mklement0 Dec 10 '22 at 15:24
3

Ctrl+t opens a new powershell tab that starts as if it was a fresh powershell session.

Colin
  • 156
  • 4
1

Try either 1 of below to clear variable memory , it shall help

exit     # Exit will quit from Powershell. 


Get-Variable -Exclude PWD,*Preference | Remove-Variable -EA 0 # this will kill all the memory on current session
HO LI Pin
  • 1,441
  • 13
  • 13
  • Thumbs up, so far this works for me.` PSVersion: 5.1.19041.1682, PSCompatibleVersions: {1.0, 2.0, 3.0, 4.0...} ` – snahl Aug 02 '22 at 07:41