3.1 Start Azure as‐a‐code configuration ‐ Powershell ISE and common Azure powershell modules - dcasota/m365-scripts GitHub Wiki
Powershell is the preferred tool for most Microsoft admins. In Windows, it comes along with the integrated developer environment, Powershell IDE. It supports out-of-the-box Powershell 5. With Powershell 7, Microsoft advises to make use of Visual Studio Code. Powershell IDE still is built-in but doesn't support Powershell 7 out-of-the-box.
The recipe from https://blog.ironmansoftware.com/using-powershell-7-in-the-windows-powershell-ise shows how to make use of Powershell 7 with Powershell IDE.
Run the following code snippet in Powershell IDE.
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Clear()
$psISE.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
}
$PowerShell = Start-Process PWSH -ArgumentList @("-NoExit") -PassThru -WindowStyle Hidden
$Runspace = New-OutOfProcRunspace -ProcessId $PowerShell.Id
$Host.PushRunspace($Runspace)
}, "ALT+F5") | Out-Null
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Switch to Windows PowerShell", {
$Host.PopRunspace()
$Child = Get-CimInstance -ClassName win32_process | where {$_.ParentProcessId -eq $Pid}
$Child | ForEach-Object { Stop-Process -Id $_.ProcessId }
}, "ALT+F6") | Out-Null
In the Powershell ISE menu, you now have the add-on option switch to Powershell 5 or to Powershell 7.
It makes sense to double check if a Powershell script has been started from Powershell 5 or Powershell 7. Here a sample.
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
}
function SwitchToPowershell7
{
$PowerShell = Start-Process PWSH -ArgumentList @("-NoExit") -PassThru -WindowStyle Hidden
$Runspace = New-OutOfProcRunspace -ProcessId $PowerShell.Id
$Host.PushRunspace($Runspace)
}
function Test-Prerequisites {
if ( $PSVersionTable.PSVersion.tostring() -lt "7.4.4" ) { SwitchToPowershell7 }
if ( $PSVersionTable.PSVersion.tostring() -lt "7.4.4" )
{
echo "Please update Powershell to at least version 7.4.4"
exit
}
}
Common Azure Powershell modules
How to uninstall Azure/M365 Powershell modules?
First, run the code snippet in a Powershell 5 console window, then, in a Powershell 7 console window.
(get-module -listavailable).name | foreach-object { if ($_ -like 'Az.*') {uninstall-module -name $_ -AllVersions -force -confirm:$false}}
(get-module -listavailable).name | foreach-object { if ($_ -like 'AzureRM.*') {uninstall-module -name $_ -AllVersions -force -confirm:$false}}
(get-module -listavailable).name | foreach-object { if ($_ -like 'Microsoft.Graph.*') {uninstall-module -name $_ -AllVersions -force -confirm:$false}}
uninstall-module -name PnP.Powershell -AllVersions -force -confirm:$false
uninstall-module -name ExchangeOnlineManagement -AllVersions -force -confirm:$false
uninstall-module -name Microsoft.Online.SharePoint.PowerShell -AllVersions -force -confirm:$false
uninstall-module -name MicrosoftTeams -AllVersions -force -confirm:$false
Avoid a mix of installing modules in Powershell 5 and Powershell 7.