Skip to content

Commit 334ee1c

Browse files
authored
Merge pull request MicrosoftDocs#1112 from sptramer/uninstall/update-script
Update uninstall script to be more correct
2 parents fbaee0f + 8c23ac4 commit 334ee1c

File tree

4 files changed

+72
-23
lines changed

4 files changed

+72
-23
lines changed

docs-conceptual/azps-1.8.0/uninstall-az-ps.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,20 @@ function Uninstall-AllModules {
6161
'Creating list of dependencies...'
6262
$target = Find-Module $TargetModule -RequiredVersion $version
6363
$target.Dependencies | ForEach-Object {
64-
if ($_.requiredVersion) {
64+
if ($_.PSObject.Properties.Name -contains 'requiredVersion') {
6565
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$_.requiredVersion}
6666
}
6767
else { # Assume minimum version
6868
# Minimum version actually reports the installed dependency
6969
# which is used, not the actual "minimum dependency." Check to
7070
# see if the requested version was installed as a dependency earlier.
71-
$candidate = Get-InstalledModule $_.name -RequiredVersion $version
71+
$candidate = Get-InstalledModule $_.name -RequiredVersion $version -ErrorAction Ignore
7272
if ($candidate) {
7373
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$version}
7474
}
7575
else {
76-
Write-Warning ("Could not find uninstall candidate for {0}:{1} - module may require manual uninstall" -f $_.name,$version)
76+
$availableModules = Get-InstalledModule $_.name -AllVersions
77+
Write-Warning ("Could not find uninstall candidate for {0}:{1} - module may require manual uninstall. Available versions are: {2}" -f $_.name,$version,($availableModules.Version -join ', '))
7778
}
7879
}
7980
}
@@ -109,12 +110,17 @@ Uninstalling Az.AnalysisServices version 0.7.0
109110
...
110111
```
111112

113+
> [!NOTE]
114+
> If this script can't match an exact dependent module version to uninstall, it won't uninstall _any_ version of that module. This is because there may be other versions
115+
> of `Az` on your system which rely on these modules. In this case, the versions of the module that couldn't be found will be listed, if any were installed. You can
116+
> then remove any old versions manually with `Uninstall-Module`.
117+
112118
Run this command for every version of Azure PowerShell that you want to uninstall. For convenience, the following
113119
script will uninstall all versions of Az __except__ for the latest.
114120

115121
```powershell-interactive
116122
$versions = (Get-InstalledModule Az -AllVersions | Select-Object Version)
117-
$versions[1..($versions.Length-1)] | foreach { Uninstall-AllModules -TargetModule Az -Version ($_.Version) -Force }
123+
$versions[0..($versions.Length-2)] | foreach { Uninstall-AllModules -TargetModule Az -Version ($_.Version) -Force }
118124
```
119125

120126
## Uninstall the AzureRM module

docs-conceptual/azps-2.2.0/uninstall-az-ps.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Uninstall Azure PowerShell
33
description: How to perform a complete uninstall of Azure PowerShell
4-
ms.date: 05/10/2019
4+
ms.date: 06/10/2019
55
author: sptramer
66
ms.author: sttramer
77
ms.manager: carmonm
@@ -61,19 +61,20 @@ function Uninstall-AllModules {
6161
'Creating list of dependencies...'
6262
$target = Find-Module $TargetModule -RequiredVersion $version
6363
$target.Dependencies | ForEach-Object {
64-
if ($_.requiredVersion) {
64+
if ($_.PSObject.Properties.Name -contains 'requiredVersion') {
6565
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$_.requiredVersion}
6666
}
6767
else { # Assume minimum version
6868
# Minimum version actually reports the installed dependency
6969
# which is used, not the actual "minimum dependency." Check to
7070
# see if the requested version was installed as a dependency earlier.
71-
$candidate = Get-InstalledModule $_.name -RequiredVersion $version
71+
$candidate = Get-InstalledModule $_.name -RequiredVersion $version -ErrorAction Ignore
7272
if ($candidate) {
7373
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$version}
7474
}
7575
else {
76-
Write-Warning ("Could not find uninstall candidate for {0}:{1} - module may require manual uninstall" -f $_.name,$version)
76+
$availableModules = Get-InstalledModule $_.name -AllVersions
77+
Write-Warning ("Could not find uninstall candidate for {0}:{1} - module may require manual uninstall. Available versions are: {2}" -f $_.name,$version,($availableModules.Version -join ', '))
7778
}
7879
}
7980
}
@@ -109,12 +110,17 @@ Uninstalling Az.AnalysisServices version 0.7.0
109110
...
110111
```
111112

113+
> [!NOTE]
114+
> If this script can't match an exact dependency with the same version to uninstall, it won't uninstall _any_ version of that dependecy. This is because there may be
115+
> other versions of the target module on your system which rely on these dependencies. In this case, the available versions of the dependency are listed.
116+
> You can then remove any old versions manually with `Uninstall-Module`.
117+
112118
Run this command for every version of Azure PowerShell that you want to uninstall. For convenience, the following
113119
script will uninstall all versions of Az __except__ for the latest.
114120

115121
```powershell-interactive
116122
$versions = (Get-InstalledModule Az -AllVersions | Select-Object Version)
117-
$versions[1..($versions.Length-1)] | foreach { Uninstall-AllModules -TargetModule Az -Version ($_.Version) -Force }
123+
$versions[0..($versions.Length-2)] | foreach { Uninstall-AllModules -TargetModule Az -Version ($_.Version) -Force }
118124
```
119125

120126
## Uninstall the AzureRM module

docs-conceptual/azurermps-5.7.0/uninstall-azurerm-ps.md

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Uninstall Azure PowerShell
33
description: How to perform a complete uninstall of Azure PowerShell
4-
ms.date: 06/20/2018
4+
ms.date: 06/10/2019
55
author: sptramer
66
ms.author: sttramer
77
ms.manager: carmonm
@@ -50,22 +50,39 @@ function Uninstall-AllModules {
5050
[Parameter(Mandatory=$true)]
5151
[string]$Version,
5252
53-
[switch]$Force
54-
)
53+
[switch]$Force,
5554
55+
[switch]$WhatIf
56+
)
57+
5658
$AllModules = @()
57-
59+
5860
'Creating list of dependencies...'
5961
$target = Find-Module $TargetModule -RequiredVersion $version
6062
$target.Dependencies | ForEach-Object {
61-
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$_.requiredversion}
63+
if ($_.PSObject.Properties.Name -contains 'requiredVersion') {
64+
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$_.requiredVersion}
65+
}
66+
else { # Assume minimum version
67+
# Minimum version actually reports the installed dependency
68+
# which is used, not the actual "minimum dependency." Check to
69+
# see if the requested version was installed as a dependency earlier.
70+
$candidate = Get-InstalledModule $_.name -RequiredVersion $version -ErrorAction Ignore
71+
if ($candidate) {
72+
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$version}
73+
}
74+
else {
75+
$availableModules = Get-InstalledModule $_.name -AllVersions
76+
Write-Warning ("Could not find uninstall candidate for {0}:{1} - module may require manual uninstall. Available versions are: {2}" -f $_.name,$version,($availableModules.Version -join ', '))
77+
}
78+
}
6279
}
6380
$AllModules += New-Object -TypeName psobject -Property @{name=$TargetModule; version=$Version}
6481
6582
foreach ($module in $AllModules) {
66-
Write-Host ('Uninstalling {0} version {1}' -f $module.name,$module.version)
83+
Write-Host ('Uninstalling {0} version {1}...' -f $module.name,$module.version)
6784
try {
68-
Uninstall-Module -Name $module.name -RequiredVersion $module.version -Force:$Force -ErrorAction Stop
85+
Uninstall-Module -Name $module.name -RequiredVersion $module.version -Force:$Force -ErrorAction Stop -WhatIf:$WhatIf
6986
} catch {
7087
Write-Host ("`t" + $_.Exception.Message)
7188
}
@@ -81,7 +98,8 @@ Uninstall-AllModules -TargetModule AzureRM -Version 4.4.1 -Force
8198
```
8299

83100
As the script runs, it will display the name and version of each submodule that is being
84-
uninstalled.
101+
uninstalled. To run the script to only see what would be deleted, without removing it,
102+
use the `-WhatIf` option.
85103

86104
```output
87105
Creating list of dependencies...
@@ -92,4 +110,16 @@ Uninstalling Azure.AnalysisServices version 0.4.7
92110
...
93111
```
94112

95-
Run this command for every version of Azure PowerShell that you want to uninstall.
113+
> [!NOTE]
114+
> If this script can't match an exact dependency with the same version to uninstall, it won't uninstall _any_ version of that dependecy. This is because there may be
115+
> other versions of the target module on your system which rely on these dependencies. In this case, the available versions of the dependency are listed.
116+
> You can then remove any old versions manually with `Uninstall-Module`.
117+
118+
119+
Run this command for every version of Azure PowerShell that you want to uninstall. For convenience, the following
120+
script will uninstall all versions of AzureRM __except__ for the latest.
121+
122+
```powershell-interactive
123+
$versions = (get-installedmodule AzureRM -AllVersions | Select-Object Version)
124+
$versions[0..($versions.Length-2)] | foreach { Uninstall-AllModules -TargetModule AzureRM -Version ($_.Version) -Force }
125+
```

docs-conceptual/azurermps-6.13.0/uninstall-azurerm-ps.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Uninstall Azure PowerShell
33
description: How to perform a complete uninstall of Azure PowerShell
4-
ms.date: 11/30/2018
4+
ms.date: 06/10/2019
55
author: sptramer
66
ms.author: sttramer
77
ms.manager: carmonm
@@ -74,19 +74,20 @@ function Uninstall-AllModules {
7474
'Creating list of dependencies...'
7575
$target = Find-Module $TargetModule -RequiredVersion $version
7676
$target.Dependencies | ForEach-Object {
77-
if ($_.requiredVersion) {
77+
if ($_.PSObject.Properties.Name -contains 'requiredVersion') {
7878
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$_.requiredVersion}
7979
}
8080
else { # Assume minimum version
8181
# Minimum version actually reports the installed dependency
8282
# which is used, not the actual "minimum dependency." Check to
8383
# see if the requested version was installed as a dependency earlier.
84-
$candidate = Get-InstalledModule $_.name -RequiredVersion $version
84+
$candidate = Get-InstalledModule $_.name -RequiredVersion $version -ErrorAction Ignore
8585
if ($candidate) {
8686
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$version}
8787
}
8888
else {
89-
Write-Warning ("Could not find uninstall candidate for {0}:{1} - module may require manual uninstall" -f $_.name,$version)
89+
$availableModules = Get-InstalledModule $_.name -AllVersions
90+
Write-Warning ("Could not find uninstall candidate for {0}:{1} - module may require manual uninstall. Available versions are: {2}" -f $_.name,$version,($availableModules.Version -join ', '))
9091
}
9192
}
9293
}
@@ -123,10 +124,16 @@ Uninstalling Azure.AnalysisServices version 0.4.7
123124
...
124125
```
125126

127+
> [!NOTE]
128+
> If this script can't match an exact dependency with the same version to uninstall, it won't uninstall _any_ version of that dependecy. This is because there may be
129+
> other versions of the target module on your system which rely on these dependencies. In this case, the available versions of the dependency are listed.
130+
> You can then remove any old versions manually with `Uninstall-Module`.
131+
132+
126133
Run this command for every version of Azure PowerShell that you want to uninstall. For convenience, the following
127134
script will uninstall all versions of AzureRM __except__ for the latest.
128135

129136
```powershell-interactive
130137
$versions = (get-installedmodule AzureRM -AllVersions | Select-Object Version)
131-
$versions[1..($versions.Length-1)] | foreach { Uninstall-AllModules -TargetModule AzureRM -Version ($_.Version) -Force }
138+
$versions[0..($versions.Length-2)] | foreach { Uninstall-AllModules -TargetModule AzureRM -Version ($_.Version) -Force }
132139
```

0 commit comments

Comments
 (0)