Private/Update-Modules.ps1
function Get-ModuleUpdate { cls Write-Host "Starting the Update Module process.." -ForegroundColor Green $modules = Get-Module -ListAvailable -Name "CredentialManager", "MicrosoftTeams", "MSOnline", "SharePointPnPPowerShellOnline","Microsoft.Online.SharePoint.PowerShell" #Get duplicate modules $duplicates = ($modules | Group-Object name -NoElement | Where-Object count -gt 1).Name if($duplicates.count -ge 1) { Write-Host "Duplicate modules found, deinstalling these first" -ForegroundColor Yellow #Go through all duplicates foreach($duplicate in $duplicates) { #Remove old versions $duplicatemodules = Get-InstalledModule $duplicate -AllVersions foreach($duplicatemodule in $duplicatemodules) { $latest = get-installedmodule $duplicatemodule.Name if($duplicatemodule.version -ne $latest.version) { Write-Host "Uninstalling module $($duplicatemodule.Name) with version $($duplicatemodule.version)" Uninstall-Module $duplicatemodule -force } } } } else { Write-Host "No duplicate modules found" -ForegroundColor Green } #declare array $myarray = @() $modules = Get-Module -ListAvailable -Name "CredentialManager", "MicrosoftTeams", "MSOnline", "SharePointPnPPowerShellOnline","Microsoft.Online.SharePoint.PowerShell" Write-Host "Comparing modules to online versions, this can take a minute" -ForegroundColor Yellow $i = 1 $activity = "Checking modules against PowerShell Gallery" foreach ($module in $modules) { Write-Progress -Activity $activity -Status "Checking module $($i.ToString().PadLeft($modules.Count.ToString().Length)) of $($modules.Count)" -CurrentOperation "Checking module $($module)" -PercentComplete ($i / $modules.count * 100) #find the current version in the gallery Try { $online = Find-Module -Name $module.name -Repository PSGallery -ErrorAction Stop } Catch { Write-Warning "Module $($module.name) was not found in the PSGallery" } #compare versions if ($online.version -gt $module.version) { $UpdateAvailable = $True } else { $UpdateAvailable = $False } $myobj = New-Object -TypeName PSObject Add-Member -InputObject $myobj -MemberType 'NoteProperty' -Name 'Module name' -Value $module.Name Add-Member -InputObject $myobj -MemberType 'NoteProperty' -Name 'Current version' -Value $module.Version Add-Member -InputObject $myobj -MemberType 'NoteProperty' -Name 'Latest version' -Value $online.Version Add-Member -InputObject $myobj -MemberType 'NoteProperty' -Name 'Update Available' -Value $UpdateAvailable $myarray += $myobj Clear-Variable myobj $i++ } Write-Progress -Activity $activity -Status "Ready" -Completed #all modules $myarray | Sort-Object Name | Format-Table * #filter modules to only have modules that need updating $needupdatemodules = $myarray | Where-Object {$_."Update Available" -eq $true} if(($needupdatemodules).Count -ge 1) { $title = "Update modules?" $message = "Do you want to update modules now?" $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes" $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No" $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no) $choice = $host.UI.PromptForChoice($title, $message, $options, 1) if ($choice -eq 0) { Update-Modules } } else { Write-Host "All modules are up-to-date, press any key to return to the menu" -ForegroundColor Green WaitAnyKey } } function Update-Modules { if (Get-CurrentPrivilege -eq $true) { $i = 1 $activity = "Downloading modules from PowerShell Gallery" foreach ($module in $needupdatemodules) { Write-Progress -Activity $activity -Status "Downloading module $($i.ToString().PadLeft($needupdatemodules.Count.ToString().Length)) of $($needupdatemodules.Count)" -CurrentOperation "Downloading module $($module)" -PercentComplete ($i / $needupdatemodules.count * 100) Update-Module $module."Module Name" -Force $i++ } Write-Progress -Activity $activity -Status "Ready" -Completed } else { Write-Host ("Please start PowerShell as administrator to install/update modules") -Fore Red; Write-Host ("The console will now exit so you can start it as an administrator") -Fore Red; WaitAnyKey; exit } } |