How to reload the PowerShell console session

During lunch with a colleague we started talking about how it would be really nice to be able to reload the current PowerShell session when working in the console. For those of us using the brilliant ISESteroids, this functionality have been available for ISE users for a little while now.

I googled the issue and didn’t find any “good” solutions to doing this. There was one using a VB-script to reload PowerShell, but I’d rather try to solve this using only PowerShell.

I thought about taking a snapshot of the environment at first launch, and then creating a function to delete everything made after that snapshot, meaning unloading any functions, removing any variables etc. But eventually I came up with a much simpler solution!

It’s so easy that it might be considered cheating, but hey, it works!

This is what I came up with:

function Invoke-PowerShell {
    powershell -nologo
    Invoke-PowerShell
}

See what I did there? The function is calling PowerShell, and then invoking itself when that session is closed. This is creating an endless loop, so you can exit out of the current session and instantly start a new fresh session to work in. Nifty eh?

But there were some additional hurdles to get it just right. First, I need to call this from my PowerShell profile, but since the profile will be loaded again in the second session, I wanted it to stop there, or I would really be loading PowerShell within PowerShell forever (I didn’t test this, but I guess I would run out of memory eventually, or something else would stop the madness).

The solution? Check the parent process, and only run the function when the parent process was NOT PowerShell itself. Here is the code used for that:

$parentProcessId = (Get-WmiObject Win32_Process -Filter "ProcessId=$PID").ParentProcessId
$parentProcessName = (Get-WmiObject Win32_Process -Filter "ProcessId=$parentProcessId").ProcessName

if ($host.Name -eq 'ConsoleHost') {
    if (-not($parentProcessName -eq 'powershell.exe')) {
        Invoke-PowerShell
    }
}

Then I just needed to tidy things up with a nice helper function with an easy to remember alias:

function Restart-PowerShell {
    if ($host.Name -eq 'ConsoleHost') {
        exit
    }
    Write-Warning 'Only usable while in the PowerShell console host'
}
Set-Alias -Name 'reload' -Value 'Restart-PowerShell'

Now, everytime I need to reload the console session, I just type reload, and voilà – a new fresh session to work in!

Here are all the code needed. Just copy/paste it into your PowerShell profile. Remember to put the last part calling the Invoke-PowerShell command right at the bottom of the profile.


function Invoke-PowerShell {
powershell nologo
Invoke-PowerShell
}
function Restart-PowerShell {
if ($host.Name -eq 'ConsoleHost') {
exit
}
Write-Warning 'Only usable while in the PowerShell console host'
}
Set-Alias Name 'reload' Value 'Restart-PowerShell'
$parentProcessId = (Get-WmiObject Win32_Process Filter "ProcessId=$PID").ParentProcessId
$parentProcessName = (Get-WmiObject Win32_Process Filter "ProcessId=$parentProcessId").ProcessName
if ($host.Name -eq 'ConsoleHost') {
if (-not($parentProcessName -eq 'powershell.exe')) {
Invoke-PowerShell
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s