UpdateAz.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 9eb5ce4f-04cf-4fec-a2b8-f424812bb556
 
.AUTHOR rustedwizard@outlook.com
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 Uninstall and install latest version of Azure Powershell and Azure Cli
 
#>
 

param()
Write-Output "*********************************************************************"
Write-Output "** Update Az **"
Write-Output "** This script will perform following operation: **"
Write-Output "** 1. Uninstall all versions of Az PowerShell. **"
Write-Output "** 2. Install the latest version of Az PowerShell available. **"
Write-Output "** 3. Upgrade/install the latest version of Azure Cli. **"
Write-Output "** 4. Upgrade all installed Azure Cli extensions. **"
Write-Output "*********************************************************************"
Write-Output ""
Write-Output ""
Write-Output "Remove all versions of Az PowerShell currently installed"
$AzVersions = Get-InstalledModule -Name Az -AllVersions -ErrorAction SilentlyContinue
if ($null -ne $AzVersions) {
    Foreach ($v in $AzVersions) {
        $temp += (Import-Clixml -Path (Join-Path -Path $v.InstalledLocation -ChildPath PSGetModuleInfo.xml)).Dependencies.Name
        Foreach ($e in $temp) {
            if ($null -ne $e) {
                if (Get-Module -ListAvailable -Name $e) {
                    Write-Output "Dependency module found: $e"
                    Remove-Module -Name $e -ErrorAction SilentlyContinue
                    Write-Output "Attempting to uninstall module: $e"
                    Uninstall-Module -Name $e -AllVersions
                } 
                else {
                    Write-Host "Dependency Module $e not installed, skipping..."
                }
            }
        }
    }
    Write-Output ""
    Write-Output "Remove Az PowerShell..."
    Remove-Module -Name Az -ErrorAction SilentlyContinue
    Uninstall-Module -Name Az -AllVersions
}else{
    Write-Host "No Az PowerShell install has been found, skip uninstallation..."
}
Write-Output "Install latest Az PowerShell..."
Install-Module -Name Az -Scope AllUsers -Repository PSGallery -Force
Write-Output "Upgrade operation has completed successfully."
Write-Output ""
Write-Output "Upgrade/Install Azure Cli"
Write-Output ""
Write-Output "Downloading latest version of Azure Cli..."
Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi
Write-Output "Installer downloaded, start unattended install..."
Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'
Write-Output "Installation process end, clean up..."
Remove-Item .\AzureCLI.msi
Write-Host "Upgrade Azure Cli Extensions..."
Az Upgrade
Write-Host "Script Execution Finished successfully."