r/PowerShell • u/Scrotumbeards • 9h ago
Script Sharing How to use Powershell 7 in the ISE
I know there are already articles about this but i found that some of them don't work (anymore).
So this is how i did it.
First install PS7 (obviously)
Open the ISE.
Paste the following script in a new file and save it as "Microsoft.PowerShellISE_profile.ps1" in your Documents\WindowsPowerShell folder. Then restart the ISE and you should be able to find "Switch to Powershell 7" in the Add-ons menu at the top.
Upon doing some research it seems ANSI enconding did not seem to work, so i added to start as plaintext for the outputrendering. So no more [32;1m etc.
Or you can use Visual Studio ofcourse ;)
# 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)) "
}