UpdateAz.ps1


<#PSScriptInfo
 
.VERSION 2.1
 
.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-Host -ForegroundColor Yellow "*********************************************************************" 
Write-Host -ForegroundColor Yellow "** Update Az **" 
Write-Host -ForegroundColor Yellow "** This script will perform following operation: **" 
Write-Host -ForegroundColor Yellow "** 1. Uninstall all versions of Az PowerShell. **" 
Write-Host -ForegroundColor Yellow "** 2. Install the latest version of Az PowerShell available. **" 
Write-Host -ForegroundColor Yellow "** 3. Upgrade/install the latest version of Azure Cli. **" 
Write-Host -ForegroundColor Yellow "*********************************************************************" 
Write-Host ""
Write-Host ""
Write-Host -ForegroundColor Yellow "Remove all versions of Az PowerShell currently installed" 
$AzVersions = Get-InstalledModule -Name Az -AllVersions -ErrorAction SilentlyContinue
if ($null -ne $AzVersions) {
    Foreach ($v in $AzVersions) {
        $version = $v.Version
        Write-Host -ForegroundColor Yellow "Removing Az PowerShell version $version"
        $temp += (Import-Clixml -Path (Join-Path -Path $v.InstalledLocation -ChildPath PSGetModuleInfo.xml)).Dependencies.Name
        $total = $temp.Count
        $count = 0
        Foreach ($e in $temp) {
            $count += 1
            if ($null -ne $e) {
                if (Get-Module -ListAvailable -Name $e) {
                    $prog = $count / $total * 100
                    Write-Progress -Activity "Removing Depedency Module..." -Status "Dependency module found: $e" -PercentComplete $prog
                    Remove-Module -Name $e -ErrorAction SilentlyContinue
                    Write-Progress -Activity "Removing Depedency Module..." -Status "Attempting to uninstall module: $e" -PercentComplete $prog
                    Uninstall-Module -Name $e -AllVersions
                } 
                else {
                    Write-Host "Dependency Module $e not installed, skipping..." -ForegroundColor Yellow
                }
            }
        }
        Write-Progress -Activity "Removing Depedency Module..." -Status "Ready" -Completed
        Write-Host -ForegroundColor Green "Successfully removed Az PowerShell version $version"
    }
    Write-Host ""
    Write-Host "Remove Az PowerShell..." -ForegroundColor Yellow
    Remove-Module -Name Az -ErrorAction SilentlyContinue
    Uninstall-Module -Name Az -AllVersions
}else{
    Write-Host "No Az PowerShell install has been found, skip uninstallation..." -ForegroundColor Yellow
}
Write-Host "Install latest Az PowerShell..." -ForegroundColor Yellow
Install-Module -Name Az -Scope AllUsers -Repository PSGallery -Force
Write-Host "Upgrade operation has completed successfully." -ForegroundColor Green
Write-Host ""
Write-Host "Upgrade/Install Azure Cli" -ForegroundColor Yellow
Write-Host ""
Write-Host "Downloading latest version of Azure Cli..." -ForegroundColor Blue
Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi
Write-Host "`Installer downloaded, start unattended install..." -ForegroundColor Yellow
Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'
Write-Host "`rInstallation process end, clean up..." -ForegroundColor Green 
Write-Host "Installation of Azure Cli completed successfully." -ForegroundColor Green
Remove-Item .\AzureCLI.msi
Write-Host "Script Execution Finished successfully." -ForegroundColor Green