Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • VirtualBox
  • VMware
  • Windows
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • VirtualBox
  • VMware
  • Windows
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Fix Get-MgUser One or More Errors Occurred

September 22, 2023
in Blog, Microsoft Graph, Powershell
0
ADVERTISEMENT

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."
Note Note: You must close all opening PowerShell window, then open a new one then run the uninstall script. Otherwise, you would get errors.

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.

aDlp10F8rOuPYrQ7vm6dWdePQYKKpW7BMzNjRQRzakjg2c9wL04ZZPmpA9ZL

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. 

Note Note: in our case, it takes one hour to complete, so please be patient.

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
hcpr4oZdpmFB86C93Kn7fTdHhQ30xZewt37mnIgDW1zH4mL1hjb8KanP5kpl

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Remove NuGet Package Provider Using PowerShell

Next Post

How to Cleanup Microsoft.Graph PowerShell Modules in Windows

Related Posts

Ftr38

[WinForms] Creating GUIs in Windows PowerShell with WinForms

November 15, 2023
Ftr21

Converting DateTime Obtained from Microsoft Graph Call to PowerShell Date and Time Format

October 21, 2023
Ftr21

Translate Microsoft 365 License GUIDs to Product Names in PowerShell Microsoft Graph

October 19, 2023
Ftr21

How to Get an Access Token for Microsoft Graph PowerShell / API

November 27, 2023
Ftr21

Getting Access Token for Microsoft Graph

November 27, 2023
Ftr5

How to Copy Files without Changing Date Creation Time on Windows

November 27, 2023

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • [WinForms] Creating GUIs in Windows PowerShell with WinForms
  • Converting DateTime Obtained from Microsoft Graph Call to PowerShell Date and Time Format
  • Translate Microsoft 365 License GUIDs to Product Names in PowerShell Microsoft Graph

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2023 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory