How to Use PowerShell 7 in PowerShell ISE

Although the PowerShell ISE is now no longer supported (apparently), you can allow it to use PowerShell 7, which can be handy if you need to use some of the new PowerShell 7 commands, not available in PowerShell 5 or 6 for example. Or just use VS Code!

Firstly ensure you have PowerShell 7 installed on the machine.

Within your profile’s Documents folder create a WindowsPowerShell folder.

So you have: c:\user\bob\Documents\WindowsPowerShell

Now drop in a file called: Microsoft.PowerShellISE_profile.ps1 and add the following to it:

# Initialize ISE object
$myISE = $psISE

# Clear any existing AddOns menu items
$myISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Clear()

# Add a menu option to switch to PowerShell 7 (pwsh.exe)
$myISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Switch to PowerShell 7", { 
    function New-OutOfProcRunspace {
        param($ProcessId)

        $ci = New-Object -TypeName System.Management.Automation.Runspaces.NamedPipeConnectionInfo -ArgumentList @($ProcessId)
        $tt = [System.Management.Automation.Runspaces.TypeTable]::LoadDefaultTypeFiles()

        $Runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($ci, $Host, $tt)
        $Runspace.Open()
        $Runspace
    }

    # Start PowerShell 7 (pwsh) process with output rendering set to PlainText
    $PowerShell = Start-Process PWSH -ArgumentList @('-NoExit', '-Command', '$PSStyle.OutputRendering = [System.Management.Automation.OutputRendering]::PlainText') -PassThru -WindowStyle Hidden
    $Runspace = New-OutOfProcRunspace -ProcessId $PowerShell.Id
    $Host.PushRunspace($Runspace)

}, "ALT+F5") | Out-Null  # Add hotkey ALT+F5

# Add a menu option to switch back to Windows PowerShell 5.1
$myISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Switch to Windows PowerShell", { 
    $Host.PopRunspace()

    # Get the child processes of the current PowerShell instance and stop them
    $Child = Get-CimInstance -ClassName win32_process | where {$_.ParentProcessId -eq $Pid}
    $Child | ForEach-Object { Stop-Process -Id $_.ProcessId }

}, "ALT+F6") | Out-Null  # Add hotkey ALT+F6

# Custom timestamp function to display before the prompt
function Write-Timestamp {
    Write-Host (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") -NoNewline -ForegroundColor Yellow
    Write-Host "  $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) $($args[0])"
}

# Customize the prompt to display a timestamp of the last command
function Prompt {
    Write-Timestamp "$(Get-History -Count 1 | Select-Object -ExpandProperty CommandLine)"
    return "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
}

Now close and open the PowerShell ISE, you can then see a new menu at top under “Add-Ons” to switch to PowerShell 7.

Leave a comment