Table of Contents
Get-MgUser One or More Errors Occurred
In some cases, you got the below error when trying to get the list of users in your organization using the Get-MgUser cmdlet with Microsoft Graph PowerShell.
PS C:\> Get-MgContext
ClientId : 14d82eec-204b-4c2f-b7e8-296a70dab67e
TenantId : c032627b-6715-4e39-9990-bcf48ee5e0c5
Scopes : {Application.Read.All, Application.ReadWrite.All, Directory.Read.All, openid...}
AuthType : Delegated
TokenCredentialType : InteractiveBrowser
CertificateThumbprint :
CertificateSubjectName :
Account : [email protected]
AppName : Microsoft Graph Command Line Tools
ContextScope : CurrentUser
PS C:\> Get-MgUser
Get-MgUser : One or more errors occurred.
At line:1 char:1
+ Get-MgUser
+ ~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-MgUser_List], AggregateException
+ FullyQualifiedErrorId : System.AggregateException,Microsoft.Graph.PowerShell.Cmdlets.GetMgUser_List
As you can see, in the above log, even we’ve connected to the Microsoft Graph PowerShell with the right permissions. We still get the error.
How to Fix Microsoft Graph One or More Errors Occurred
The root of the issue is we’ve installed two versions of Microsoft Graph PowerShell SDK module, and these modules are conflict. You can check it on your machine using Get-Module cmdlet.
PS C:\> Get-Module Microsoft.Graph -ListAvailable
Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 2.6.1 Microsoft.Graph
Manifest 2.5.0 Microsoft.Graph
When we check the imported modules in the current session. We saw two versions of the module Microsoft.Graph.Authentication are imported.
PS C:\> Get-Module | select Name, Version, ModuleType
Name Version ModuleType
---- ------- ----------
Microsoft.Graph.Authentication 2.6.1 Script
Microsoft.Graph.Authentication 2.5.0 Script
Microsoft.PowerShell.Management 3.1.0.0 Manifest
Microsoft.PowerShell.Utility 3.1.0.0 Manifest
PackageManagement 1.4.8.1 Script
PowerShellGet 2.2.5 Script
PSReadLine 2.0.0 Script
To fix the problem, we need to remove all installed versions of the module then install the latest one. But uninstalling the Microsoft Graph module is a little bit complicated.
So, we’ve created a PowerShell script to do it automatically. Below is a PowerShell script do remove all installed Microsoft PowerShell SDK version then reinstall it 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.
#Required running with elevated right.
if (-not([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning "You need to have Administrator rights to run this script!`nPlease re-run this script as an Administrator in an elevated powershell prompt!"
break
}
#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
}
}
#Fix installed modules
$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
}
Write-Host "`nInstalling the Microsoft Graph PowerShell module..."
Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -Force
Write-Host "Done."
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 and the latest version is installed as well.
PS C:\> Get-Module Microsoft.Graph -ListAvailable
Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 2.6.1 Microsoft.Graph
Reinstall PowerShell 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/reinstall | iex
Not a reader? Watch this related video tutorial: