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?
-
See also: https://stackoverflow.com/q/7338395/7571258 – zett42 Dec 03 '20 at 19:36
3 Answers
First, the obligatory notice: The PowerShell ISE is no longer actively developed and there are reasons not to use it (bottom section), notably not being able to run PowerShell [Core] 6+. The actively developed editor that offers the best PowerShell development experience, across platforms, is Visual Studio Code, combined with its PowerShell extension.
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 do this in Visual Studio Code - both in the current tab and 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 automatically asked if you want to start a new session, which runs in a new powershell.exe / pwsh child process.
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 temporaryand turn on thePowerShell > Debugging: Create Temporary Integrated Consolesetting.Or: Add
"powershell.debugging.createTemporaryIntegratedConsole": truedirectly to yoursettings.jsonfile.
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 a new temporary console simply replaces the old one.
- Curiously, as of extension version
2020.6.0, you cannotexitout of a PowerShell Integrated Console, but you can also use the trash-can icon to kill it, 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$iincrements every time, across runs.Starting a new session for every debugging run avoids this problem.
- 312,089
- 56
- 508
- 622
-
@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
Ctrl+t opens a new powershell tab that starts as if it was a fresh powershell session.
- 146
- 4
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
- 1,066
- 9
- 10