Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refreshing/Restarting PowerShell session w/out exiting

I have been tweaking some of the scripts in my PowerShell profile and it has got annoying to exit powershell then restart it so it will load any changes I have made to the scripts in my profile. Is it possible to restart the powershell session without exiting?

like image 290
shreddish Avatar asked Jul 18 '12 16:07

shreddish


People also ask

How do I stop PowerShell from exiting?

PowerShell NoExit switch prevents the PowerShell console window from closing after running the script. When you double click on the PowerShell script file, or run with the PowerShell option, the script will execute quickly and then disappear.

How do I relaunch PowerShell?

You can use the “Run as Administrator” option to open a command prompt in administrator privileges. Restart-Computer cmdlet in PowerShell reboots the local computer.

How do I reset Windows PowerShell settings?

1: Reset PowerShell or Command Prompt to Default Settings If you know what setting you changed, you can revert by right-clicking on the top of a Powershell or Command Prompt window and click on Properites. Look for the setting you want to change. If you're not sure what was changed, click on Defaults.

How do you refresh an environment variable in PowerShell?

To refresh the environment variables in PowerShell, retrieves the environment variables and assign them $Env:Path to reload the environment variable path in the PowerShell. After reloading the path in PowerShell, you don't need to restart the PowerShell ISE or terminal.


2 Answers

You can just do . $profile to source the profile again.

like image 191
manojlds Avatar answered Sep 20 '22 21:09

manojlds


This will start a new session, reloading your profile, in the same console window:

Invoke-Command { & "powershell.exe" } -NoNewScope # PowerShell 5 Invoke-Command { & "pwsh.exe"       } -NoNewScope # PowerShell 7 

Or a longer but more robust command, the following should work correctly across versions:

Get-Process -Id $PID | Select-Object -ExpandProperty Path | ForEach-Object { Invoke-Command { & "$_" } -NoNewScope } 

You will, of course, lose all your variables etc. from the previous session.

like image 26
nmbell Avatar answered Sep 19 '22 21:09

nmbell