-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateUninstallModules.ps1
More file actions
68 lines (47 loc) · 3.5 KB
/
UpdateUninstallModules.ps1
File metadata and controls
68 lines (47 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#Works in parallel. However Update and uninstall verbosity aren't in sequence. I want it to wait till Azmodule update completes and uninstall child modules in one go.
#Test on Terminal.
#Remove Scope and parallel while running on PS5.1.
#Ran sucessfully in updating. However, didn't uninstall. Had to run again. Add '-Wait' parameter so it waits till update finishes to uninstall
Start-Process pwsh -ArgumentList "-noprofile" -Verb RunAs #Run PowerShell without profile with elevation
#region
#Get-InstalledPSResource -Scope AllUsers #|% {Update-PSResource -Scope AllUsers -Verbose}
#Update-PSResource -Scope AllUsers -Verbose
Update-PSResource -Scope AllUsers -Verbose -TrustRepository -Repository MAR
#endregion
#region v24 for Az modules
# Get all Az modules and their submodules
$azModules = Get-Module -ListAvailable #Az.*,Microsoft.*
# Group modules by name to handle all versions of each submodule
$groupedModules = $azModules | Group-Object -Property Name
foreach ($moduleGroup in $groupedModules) {
$moduleName = $moduleGroup.Name
$moduleVersions = $moduleGroup.Group | Select-Object -Property Name, Version, ModuleBase -Unique
if ($moduleVersions.Count -gt 1) {
# Sort versions and exclude the latest one
$versionsToRemove = $moduleVersions | Sort-Object Version | Select-Object -SkipLast 1
foreach ($version in $versionsToRemove) {
Write-Host "Removing older version: $($version.Name) : $($version.Version) → $($version.ModuleBase)" -ForegroundColor Yellow
#Write-Host "Module Path: $($version.ModuleBase)" -ForegroundColor Yellow
# Remove the folder of the older version
try {
Remove-Item -Recurse -Force -Path $version.ModuleBase
Write-Host "Successfully removed $($version.Name) version $($version.Version)" -ForegroundColor Green
} catch {
Write-Host "Failed to remove $($version.Name) version $($version.Version)" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
}
}
} else {
Write-Host "Only one version of $moduleName is installed. No action needed." -ForegroundColor Cyan
}
}
Remove-Item -Recurse -Force -Path $version.ModuleBase -ErrorAction Stop && Write-Host "Successfully removed $($version.Name) version $($version.Version)" -ForegroundColor Green
ri -r -fo $version.ModuleBase -ea Stop && wh "Successfully removed $($version.Name) version $($version.Version)" -f Green
#endRegion
$WarningPreference='silentlyContinue'
Get-InstalledModule | ForEach-Object -Parallel {$Name= $PSItem.Name; $Vpresent=$PSItem.Version; $Vavailable=$(Find-Module $Name).Version #Get Variables so parallel works fine
if($Vavailable -gt $Vpresent) {Write-Host "Updating $Name at $($PSItem.InstalledLocation) from $Vpresent to $Vavailable" -ForegroundColor Magenta; Update-Module -Name $Name -Scope AllUsers -Force}
else {Write-Host "$Name's version $Vpresent is up-to-date." -ForegroundColor Green} #Update outdated module
Get-InstalledModule $Name -AllVersions|Where-Object {$_.Version -lt $Vpresent}|ForEach-Object {Write-Host "- Uninstalling $Name version $($PSItem.Version)..." -ForegroundColor Magenta; $PSItem |
Uninstall-Module -Force -Verbose} #Uninstall older versions
}