Table of Contents
Uninstall Microsoft Graph PowerShell Modules Completely
In some cases, you no longer work with Microsoft Graph PowerShell and want to remove them from your computer completely. Or you want to uninstall the older versions of the module to install the latest version.
First, we get the list of the installed modules with all versions. As you can see below, over time, when update the module, the older versions are not got removed automatically. So, we’ve six versions of the module.
Get-Module Microsoft.Graph.Authentication -ListAvailable | select Name, Version
Name Version ModuleType
---- ------- ----------
Microsoft.Graph.Authentication 2.5.0 Script
Microsoft.Graph.Authentication 1.28.0 Script
Microsoft.Graph.Authentication 1.27.0 Script
Microsoft.Graph.Authentication 1.25.0 Script
Microsoft.Graph.Authentication 2.4.0 Script
Microsoft.Graph.Authentication 2.3.0 Script
Remove the Microsoft Graph PowerShell module is a little bit complicated. But don’t worry, we’ve created a PowerShell script to do it automatically. The script will:
- Uninstall all Microsoft Graph modules except Microsoft.Graph.Authentication (because this module is the dependency module of others one. So, it can be uninstalled when all other modules are got removed only).
- When uninstalling the modules, some modules cannot be uninstalled because the dependencies. So, we need to remove them with Get-InstalledModule results.
- Finally, once all other modules got removed, we can uninstall the Microsoft,Graph.Authentication module.
Below is a PowerShell script do remove all installed Microsoft PowerShell SDK version on your computer.
1️⃣ To do it, copy the below script => Open Notepad (or any your favorite text editor) => Paste the script then save it as a .ps1 file. The file format of a PowerShell script is *.ps1.
#Uninstall Microsoft.Graph modules except Microsoft.Graph.Authentication
$Modules = Get-Module Microsoft.Graph* -ListAvailable |
Where-Object {$_.Name -ne "Microsoft.Graph.Authentication"} | Select-Object Name -Unique
Foreach ($Module in $Modules){
$ModuleName = $Module.Name
$Versions = Get-Module $ModuleName -ListAvailable
Foreach ($Version in $Versions){
$ModuleVersion = $Version.Version
Write-Host "Uninstall-Module $ModuleName $ModuleVersion"
Uninstall-Module $ModuleName -RequiredVersion $ModuleVersion -ErrorAction SilentlyContinue
}
}
#Uninstall the modules cannot be removed from first part.
$InstalledModules = Get-InstalledModule Microsoft.Graph* |
Where-Object {$_.Name -ne "Microsoft.Graph.Authentication"} | Select-Object Name -Unique
Foreach ($InstalledModule in $InstalledModules){
$InstalledModuleName = $InstalledModule.Name
$InstalledVersions = Get-Module $InstalledModuleName -ListAvailable
Foreach ($InstalledVersion in $InstalledVersions){
$InstalledModuleVersion = $InstalledVersion.Version
Write-Host "Uninstall-Module $InstalledModuleName $InstalledModuleVersion"
Uninstall-Module $InstalledModuleName -RequiredVersion $InstalledModuleVersion -ErrorAction SilentlyContinue
}
}
#Uninstall Microsoft.Graph.Authentication
$ModuleName = "Microsoft.Graph.Authentication"
$Versions = Get-Module $ModuleName -ListAvailable
Foreach ($Version in $Versions){
$ModuleVersion = $Version.Version
Write-Host "Uninstall-Module $ModuleName $ModuleVersion"
Uninstall-Module $ModuleName -RequiredVersion $ModuleVersion
}
2️⃣ Right click on the Windows start icon then select Windows PowerShell (Terminal) Admin.
3️⃣ Run the saved script. For example, the path of our script is E:\scripts\graph_cleanup.ps1.
4️⃣ (Optional) In some cases, when run the script, you would get the error the script cannot be loaded because running script is disabled on this sysstem.
To fix it, we need to enable running script on your system using the below command. You can get more about the Execution Policy from this post.
Set-ExecutionPolicy RemoteSigned -Force
If you get the below errors while uninstalling the Microsoft Grap module. Let’s close all opening PowerShell window (or restart your computer) the re-run the script.
WARNING: The version ‘2.4.0’ of module ‘Microsoft.Graph.Beta.Identity.DirectoryManagement’ is currently in use. Retry the operation after closing the applications.
PackageManagement\Uninstall-Package : Module ‘Microsoft.Graph.Beta.Identity.SignIns’ is in currently in use or you don’t have the required permissions.
5️⃣ The script will uninstall the modules automatically.
6️⃣ Once done, you can run the below command again to check all modules have been uninstalled.
PS C:\> Get-Command -Module Microsoft.Graph* | measure
Count : 0
Average :
Sum :
Maximum :
Minimum :
Property :
Reinstall the Microsoft PowerShell module
To reinstall the latest version of Microsoft Graph module, run the below command:
Install-Module Microsoft.Graph -force
Install-Module Microsoft.Graph.Beta -Force
Uninstall script
If you don’t want to create your own PowerShell script manually. We’ve created a PowerShell script for you. All you need to do is open PowerShell then execute the below command.
irm bonguides.com/graph/uninstall | iex
Not a reader? Watch this related video tutorial:
Really appreciated this tutorial and associated script: it worked perfectly for me.
FYI, I originally found this information on your channel on YouTube. However, there was nothing the text of that video that showed how to acquire the script itself (e.g. “Visit URL to download a copy of the script and find hundreds of other useful PowerShell tips and tricks.” I recommend you consider adding this to this and all other YouTube videos to drive more traffic to your site.
Thanks again!
So nice of you. Thanks for your idea.
thanks and this worked well for me
Glad it helped.