如何在PowerShell中使用Get-Mguser

Jacki

Microsoft Graph PowerShell中的Get-Mguser CMDLET在Microsoft Entra ID中檢索所有用戶詳細信息。您可以從組織中獲取所有Microsoft 365用戶或特定用戶。雖然您可以在Microsoft Entra Admin Center或Microsoft 365 Admin Center中獲取所有用戶,但您始終可以做更多的事情,並且可以使用PowerShell。在本文中,您將學習如何在PowerShell中使用Get-Mguser cmdlet。

開始之前

在進行進一步之前,您必須安裝並連接到Microsoft Graph PowerShell,這一點很重要。否則,Get-Mguser cmdlet將無法使用。

安裝Microsoft Graph PowerShell

以管理員的身份運行PowerShell並安裝Microsoft Graph PowerShell模塊。

Install-Module Microsoft.Graph -Force

重要的:在運行CMDLET或腳本以防止錯誤和錯誤結果之前,請務必更新到最新的Microsoft Graph PowerShell模塊版本。

連接到Microsoft Graph PowerShell

您需要使用正確的權限連接到Microsoft Graph PowerShell。如果不這樣做,則無法使用Get-Mguser CMDLET檢索用戶結果。

Connect-MgGraph -Scopes "User.Read.All", "AuditLog.Read.All"

您是否要在沒有用戶交互的情況下連接,因為您希望腳本自動運行?用基於證書的身份驗證或客戶秘密設置它。在文章中閱讀更多信息,請連接到Microsoft Graph PowerShell。

獲取用戶信息

讓我們首先從基礎知識開始,這就是獲取用戶信息。

更多閱讀:如何與PowerShell發送電子郵件

獲取單個用戶信息

要獲取用戶信息,請使用 - 用戶參數並附加用戶ID。

Get-MgUser -UserId "944d57a0-0d24-4d55-ac5b-e9b741be9031"

您也可以使用用戶principalname獲取用戶信息。

Get-MgUser -UserId "[email protected]"

添加格式列表cmdlet獲取屬性列表。

Get-MgUser -UserId "[email protected]" | Format-List

獲取所有用戶信息

運行Get-mguser cmdlet,包括-全部參數,檢索所有用戶。

筆記:始終使用-全部參數以獲取所有結果。否則,只會出現100個項目。

Get-MgUser -All

為了計算所有用戶,我們將添加測量對象命令的cmdlet。

Get-MgUser -All | Measure-Object | Select-Object -ExpandProperty Count

獲取用戶帳戶狀態

要獲得用戶帳戶狀態,我們必須添加-財產參數,包括AccountEnabled屬性。否則,AccountEnabled值顯示為空。

獲取單個用戶帳戶狀態

獲取單個用戶的帳戶狀態。

Get-MgUser -UserId "[email protected]" -Property Id, DisplayName, UserPrincipalName, AccountEnabled | Select-Object Id, DisplayName, UserPrincipalName, AccountEnabled

獲取所有用戶帳戶狀態

獲取所有用戶帳戶狀態。

Get-MgUser -All -Property Id, DisplayName, UserPrincipalName, AccountEnabled | Select-Object Id, DisplayName, UserPrincipalName, AccountEnabled

您隨時可以添加-篩選參數,僅檢索啟用帳戶。

Get-MgUser -All -Filter "accountEnabled eq true" -Property Id, DisplayName, UserPrincipalName, AccountEnabled | Select-Object Id, DisplayName, UserPrincipalName, AccountEnabled

或僅過濾禁用的用戶帳戶。

Get-MgUser -All -Filter "accountEnabled ne true" -ConsistencyLevel eventual -CountVariable CountVar -Property Id, DisplayName, UserPrincipalName, AccountEnabled | Select-Object Id, DisplayName, UserPrincipalName, AccountEnabled

要知道您可以添加哪個屬性以從Get-Mguser CMDLET獲取用戶屬性信息,請使用Microsoft文檔表

獲取所有云用戶

如果您有混合環境,則將用戶從本地廣告同步到Microsoft Entra ID。本地廣告是您的域名授權,您應該在那裡創建用戶。但是,組織中可能有一些用戶直接在雲中創建。

獲取包括客人在內的所有云用戶

讓我們僅在雲用戶上過濾。這包括來賓帳戶。

筆記:使用-篩選帶有的參數這是操作員。默認情況下不支持此請求,因為這是僅在高級查詢中支持操​​作員。因此,您必須添加一致性標題設置為最終並使用Countvar查詢字符串。

Get-MgUser -All -Filter "OnPremisesSyncEnabled ne true" -ConsistencyLevel eventual -CountVariable CountVar

讓所有云用戶不包括客人

現在,讓我們僅過濾沒有客人帳戶的雲用戶。

Get-MgUser -All -Filter "OnPremisesSyncEnabled ne true and UserType eq 'Member'" -ConsistencyLevel eventual -CountVariable CountVar

獲取所有云的訪客用戶

僅對雲中的客人用戶過濾。

Get-MgUser -All -Filter "OnPremisesSyncEnabled ne true and UserType eq 'Guest'" -ConsistencyLevel eventual -CountVariable CountVar

獲取許可用戶

並非所有用戶都獲得許可。租戶中有或沒有許可證的用戶。要確切知道哪些用戶帳戶已分配了許可證,哪些未分配了許可證,我們可以對此進行特定過濾。

獲取所有許可用戶

僅在許可用戶上過濾。

Get-MgUser -All -Filter "assignedLicenses/`$count ne 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable Records

獲取所有無執照的用戶

僅在無執照的用戶上過濾。

Get-MgUser -All -Filter "assignedLicenses/`$count eq 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable Records

獲取所有許可和阻止用戶

您可以阻止用戶,他們仍然有許可證。讓我們通過許可證獲得所有被阻止的用戶。

Get-MgUser -All -Filter "assignedLicenses/`$count ne 0 and accountEnabled eq false" -ConsistencyLevel eventual -CountVariable Records

讓本地同步用戶

僅獲取與云同步並在顯示名稱上對其進行排序的本地用戶。

Get-MgUser -All -Filter "OnPremisesSyncEnabled eq true" | Sort-Object DisplayName

獲取用戶的經理

並非所有用戶都有經理,但是其中一些用戶會這樣做。我們喜歡了解用戶或所有用戶的經理。

獲取單個用戶的經理

讓我們檢查一下單個用戶的經理是誰。

如果您運行下面的CMDLET來檢索單個用戶的經理,則將看不到經理。

Get-MgUser -UserId "[email protected]" | Select-Object DisplayName, Manager

您會看到它顯示了Microsoft.graph.powershell.models.microsoftgraphdirectoryobject的值。

DisplayName   Manager
-----------   -------
Amanda Morgan Microsoft.Graph.PowerShell.Models.MicrosoftGraphDirectoryObject

那是因為您需要添加- expandproperty範圍。

Get-MgUser -UserId "[email protected]" -ExpandProperty Manager | Select-Object @{Name = 'Manager'; Expression = { $_.Manager.AdditionalProperties.mail } }

另一種方法是將管理器屬性存儲在變量中,然後顯示。

$user = Get-MgUser -UserId "[email protected]" -ExpandProperty Manager
$user.Manager.AdditionalProperties.mail

獲取所有用戶的經理

要知道將哪個經理分配給哪個用戶,我們可以獲取所有用戶及其經理的列表。

Get-MgUser -All -ExpandProperty Manager | Select-Object UserPrincipalName, @{Name = 'Manager'; Expression = { $_.Manager.AdditionalProperties.mail } }

獲取以顯示名稱開頭的用戶

我們可以添加Startswith操作員並檢索以顯示名稱開頭的所有結果。

Get-MgUser -All -Filter "startsWith(DisplayName,'Amanda')"

您還可以添加一個字母而不是單詞。

Get-MgUser -All -Filter "startsWith(DisplayName,'A')"

獲取以郵件地址結束的用戶

添加末日操作員並檢索所有以特定郵件地址結尾的用戶,然後在顯示名稱上對其進行排序。

Get-MgUser -All -Filter "endsWith(mail,'exoip.com')" -Sort "displayName" -ConsistencyLevel eventual -CountVariable CountVar

搜索以特定郵件地址結束的兩個域。

Get-MgUser -All -Filter "endsWith(mail,'exoip.com') or endsWith(mail,'tajran.com')" -Sort "displayName" -ConsistencyLevel eventual -CountVariable CountVar

獲取用用戶主名稱結束的用戶

添加末日操作員並檢索所有以特定用戶principalname結尾的用戶,然後對顯示名稱進行排序。

Get-MgUser -All -Filter "endsWith(userprincipalname,'exoip.com')" -Sort "displayName" -ConsistencyLevel eventual -CountVariable CountVar

讓用戶登錄活動

最好在最後一次成功簽名的用戶過濾是一件好事。因此,我們可以理解該帳戶是否正在使用。

獲取單用戶登錄活動

檢查單個用戶的登錄活動。

Get-MgUser -UserId "944d57a0-0d24-4d55-ac5b-e9b741be9031" -Property SignInActivity | Select-Object -ExpandProperty SignInActivity

如果要使用userprincipalname進行- 用戶參數,您將獲得錯誤。那是因為並非所有屬性都接受。

Get-MgUser_Get: Get By Key only supports UserId and the key has to be a valid Guid

Status: 400 (BadRequest)
ErrorCode: 400

您可以做的是使用第二個Get-Mguser CMDLET檢索用戶ID,然後將其添加到第一個Get-Mguser CMDLET中。

Get-MgUser -UserId (Get-MgUser -UserId "[email protected]").Id -Property SignInActivity | Select-Object -ExpandProperty SignInActivity | fl

結果出現。

LastNonInteractiveSignInDateTime  : 20/01/2024 05:23:30
LastNonInteractiveSignInRequestId : 27e83e67-4c1c-4f74-babb-a55791680900
LastSignInDateTime                : 19/01/2024 11:31:20
LastSignInRequestId               : 6f659c53-cf13-4b7d-9fe5-93c6f9a52100
LastSuccessfulSignInDateTime      : 20/01/2024 05:23:30
LastSuccessfulSignInRequestId     : 27e83e67-4c1c-4f74-babb-a55791680900
AdditionalProperties              : {}

獲取所有用戶登錄活動

讓所有用戶最後一次成功登錄日期和時間,然後將輸出發送到與單獨窗口中的交互式表離格視圖cmdlet。

Get-MgUser -All -Property Id, UserPrincipalName, DisplayName, SignInActivity | Select-Object Id, UserPrincipalName, DisplayName, @{Name = 'LastSuccessfulSignInDateTime'; Expression = { $_.SignInActivity.LastSuccessfulSignInDateTime } } | Out-GridView -Title "Last successful sign-in date"

獲取具有公司名稱的用戶

您可以在組織中具有不同的公司名稱。很高興知道哪個用戶屬於哪個公司或在特定公司上過濾以縮小結果。

獲取單個用戶的公司名稱

獲取單個用戶的公司名稱。

Get-MgUser -UserId "[email protected]" -Property DisplayName, UserPrincipalName, CompanyName | Select-Object DisplayName, UserPrincipalName, CompanyName

獲取所有用戶的公司名稱

獲取所有用戶帳戶,包括公司名稱。

Get-MgUser -All -Property DisplayName, UserPrincipalName, CompanyName | Select-Object DisplayName, UserPrincipalName, CompanyName

添加-篩選參數和Startswith操作員檢索以特定公司名稱開頭的所有結果。

Get-MgUser -All -Property DisplayName, UserPrincipalName, CompanyName -ConsistencyLevel eventual -Count userCount -Filter "startsWith(CompanyName, 'Exo')" | Select-Object DisplayName, UserPrincipalName, CompanyName

您可以使用-搜尋參數可以在公司上搜索並獲得結果。

Get-MgUser -All -Search "CompanyName:Exoip" -ConsistencyLevel eventual

獲取具有公司名稱和帳戶的用戶

添加-搜尋查找公司名稱的參數和-篩選僅檢索啟用帳戶的參數。

Get-MgUser -All -Filter "accountEnabled eq true" -Search "CompanyName:Exoip" -ConsistencyLevel eventual

了解如何在PowerShell腳本中添加Get-Mguser cmdlet的一種絕佳方法是瀏覽PowerShell腳本示例:

  • 使用PowerShell創建Microsoft Entra ID用戶
  • 用PowerShell導出Microsoft Entra ID用戶到CSV
  • 來自共享郵箱的塊登錄
  • 與PowerShell的Microsoft 365強制簽名用戶
  • 如何從Microsoft Entra中的應用中刪除權限
  • 導出Microsoft 365殘疾用戶報告
  • 導出Microsoft 365非活動用戶報告

結論

您學會瞭如何在PowerShell中使用Get-Mguser。 Get-Mguser CMDLET是從Microsoft Entra ID和Microsoft 365中檢索用戶的絕佳CMDLET。使用特定參數或組合它們根據希望結果出現的方式過濾搜索結果。

您喜歡這篇文章嗎?您可能還喜歡如何導出條件訪問策略。不要忘記關注我們並分享這篇文章。