Table of Contents
In some cases, you wanted to check the number of commands available in the Microsoft Graph PowerShell Modules. You can use the Get-Command cmdlet, push them into a pipeline then use the measure parameter.
Get-Command -Module Microsoft.Graph* | measure
As you can see, in our computer the total number of cmdlets is so much. Over 100 thousand commands for a module? It’s not sensible.
PS C:\> Get-Command -Module Microsoft.Graph* | measure
Count : 118135
Average :
Sum :
Maximum :
Minimum :
Property :
That seemed a bit much and we turned out, we had multiple Versions of Microsoft Graph PowerShell Module installed. It could be when we update the new versions of the module. The older versions are not got removed automatically,
Get-Module Microsoft.Graph.Authentication -ListAvailable | select Name, Version
Name Version
---- -------
Microsoft.Graph.Authentication 2.5.0
Microsoft.Graph.Authentication 2.6.1
Microsoft.Graph.Authentication 2.4.0
Microsoft.Graph.Authentication 2.3.0
Microsoft.Graph.Authentication 1.28.0
Microsoft.Graph.Authentication 1.27.0
Microsoft.Graph.Authentication 1.25.0
Cleanup Microsoft.Graph PowerShell Modules
Below is a PowerShell script do remove all installed Microsoft PowerShell SDK version on your computer. We recommend you remove all installed versions then install the latest version.
Because the Microsoft.Graph.Authentication module is the dependency module of other one. So, it can be uninstalled after all other one got removed.
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.
Get-Command -Module Microsoft.Graph* | measure
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
When the modules are installed, you can check now for the available commands. This time it looks more sensible. This module has nearly 8000 commands instead of 100 thousand commands.
PS C:\> Get-Command -Module Microsoft.Graph* | measure
Count : 7394
Average :
Sum :
Maximum :
Minimum :
Property :
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: