I'm having an issue where I keep getting the error message stating that 'Get-MgUser' is not recognized as a cmdlet, function, script file, or operable program. I'm also seeing a similar message when trying to use Update-MgUser. I've already installed the Microsoft Graph PowerShell module and am using the following script to connect and update user phone numbers:
```powershell
# Connect to Microsoft Graph
Connect-MgGraph -Scope User.ReadWrite.All
# Read the CSV file
$users = Import-Csv -Path "C:Tempnumbers2.csv"
# Loop through each user in the CSV to update the PhoneNumber
foreach ($user in $users) {
$userPrincipalName = $user.UserPrincipalName
$PhoneNumber = $user.PhoneNumber
if ([string]::IsNullOrEmpty($PhoneNumber)) {
Write-Host "PhoneNumber is empty for user '$userPrincipalName'. Skipping update." -ForegroundColor Yellow
continue
}
$existingUser = Get-MgUser -UserId $userPrincipalName -ErrorAction SilentlyContinue
if ($existingUser) {
if ($existingUser.PhoneNumber -eq $PhoneNumber) {
Write-Host "User '$userPrincipalName' already has PhoneNumber '$PhoneNumber'." -ForegroundColor Cyan
} else {
Update-MgUser -UserId $userPrincipalName -PhoneNumber $PhoneNumber
Write-Host "User '$userPrincipalName' updated PhoneNumber to '$PhoneNumber' successfully." -ForegroundColor Green
}
} else {
Write-Host "User '$userPrincipalName' not found. PhoneNumber field is empty." -ForegroundColor Yellow
}
}
```
2 Answers
What version of the Microsoft Graph module are you using? If it's 2.26.*, consider downgrading to 2.25 since a lot of users have reported issues with the latest version. That might help resolve your issue!
It looks like the error happens when the Microsoft Graph PowerShell session isn't set up properly. Try running the command `Connect-MgGraph -Scope User.ReadWrite.All` again and see if any error messages pop up during that step.
I initially used 1.0 as advised by the Microsoft site, but got the same error. Then I switched to the beta version and am currently on 2.26.1. The connect command runs fine for me though; do you know how I can downgrade to 2.25?