'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?



Solution 1:[1]

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

Solution 2:[2]

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.

Note: creating new sessions in the same window like this will leave the parent process running in the background. Generally, this is not a problem as the parent (and any other ancestors) will terminate when the window is closed. If this is a concern, however, you may want to add something like the following to your profile:

$p = Get-Process -Id $PID
If ($p.Parent.Name -eq $p.Name -and !($p.MainWindowTitle))
{
    Stop-Process -Id $p.Parent.Id -Force
}

Solution 3:[3]

@manojlds' answer is right, but it could end up throwing errors. For example, if you've defined a new PSDrive in your profile, then re-dotsourcing it may cause errors.

An alternative approach is to first start powershell, then immediately start another version inside by just typing PowerShell. I make the changes to my profile in the nested console, exit, then rerun PowerShell to test the updated profile.

Another thing - make profile changes slowly and carefully. In my view, while profiles do need to evolve, that evolution typically should be slow. YMMV!!

Solution 4:[4]

You can make a simple function and add it to your $profile (or make a module and then import into '$profile'), for example:

function Restart-PowerShell{
    Start-Process PowerShell # Launch PowerShell host in new window
    exit # Exit existing PowerShell host window
}
# Add any alias if you want, for ex. rps (rp already occupied by "Remove-ItemProperty”)
Set-Alias -Name rps -Value Restart-PowerShell

Note: While this may not be exactly what you wrote in the title of your question "w/out exiting", but if, in substance, your original intention was to have some kind of restart PowerShell host command (which is not available in PS by default) then it will do the work...

Solution 5:[5]

Here is an expansion upon @Bad's answer. This will now handle ISE (because I use it all the time).

The idea is if you are in ISE, it will restart ISE. If you are in plain PowerShell, it will restart plain PowerShell. Get-PSHostProcessInfo returns information about the current PowerShell session which allows you to determine whether or not you are running ISE or not.

function Restart-PowerShell
{
    if((Get-PSHostProcessInfo).ProcessName.Contains('powershell_ise'))
    {
        Start-Process 'powershell_ise.exe'
    } else {
        Start-Process 'powershell.exe'
    }
    exit
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 manojlds
Solution 2
Solution 3 Thomas Lee
Solution 4
Solution 5 Kellen Stuart