导入Microsoft.Graph.Applications后,Get-MgUser/New-MgUser命令无法识别如何解决?
New-MgUser或Get-MgUser时提示命令未识别 已执行Import-Module Microsoft.Graph.Applications命令,并使用多个作用域运行Connect-MgGraph完成连接,但运行New-MgUser时收到以下错误:
Get-MgUser: The term 'Get-MgUser' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
解决方法
导入正确的模块:
New-MgUser和Get-MgUser属于Microsoft.Graph.Users模块,而非Microsoft.Graph.Applications。执行以下命令导入模块:Import-Module Microsoft.Graph.Users若需要使用多个Graph模块,也可一次性导入完整模块(按需选择):
Import-Module Microsoft.Graph -Force确认模块已安装:如果未安装
Microsoft.Graph.Users模块,先执行安装命令(根据权限选择作用域):# 当前用户安装 Install-Module Microsoft.Graph.Users -Scope CurrentUser # 所有用户安装(需管理员权限) Install-Module Microsoft.Graph.Users -Scope AllUsers可先执行
Get-Module -ListAvailable Microsoft.Graph.Users检查模块是否已存在。更新到最新模块版本:旧版本模块可能存在命令缺失,执行更新命令:
Update-Module Microsoft.Graph.Users验证连接作用域:确保
Connect-MgGraph时包含用户操作所需的权限作用域,例如:Connect-MgGraph -Scopes "User.ReadWrite.All", "Directory.ReadWrite.All"(注:权限不足会导致命令执行失败,但不会触发“命令未识别”错误,此步骤为后续操作提前验证)
内容的提问来源于stack exchange,提问作者Harry

