GraphPowerShellを使用して管理アカウントを一括取得する方法について記載する。管理アカウントの棚卸しの際に使用してほしい。
検証環境について
動作確認した検証環境は以下の通り。
OS:Microsoft Windows 11 Enterprise
OSbuildバージョン:10.0.22621
PowerShellのバージョン:7.4.0
GraphPowershellのモジュール名とバージョン
Microsoft.Graph.Authentication:2.13.1
Microsoft.Graph.Identity.DirectoryManagement:2.13.1サインインアカウントの権限はグローバル管理者。
使用するコマンド
Get-MgDirectoryRole
使用例
Get-MgDirectoryRole -DirectoryRoleId 97582496-a64f-4dc5-970d-71330cc3026b | Format-List取得できる情報例
DeletedDateTime :
Description : Can manage all aspects of Microsoft Entra ID and Microsoft services that use Microsoft Entra ide
ntities.
DisplayName : Global Administrator
Id : 97582496-a64f-4dc5-970d-71330cc3026b
Members :
RoleTemplateId : 62e90394-69f5-4237-9190-012177145e10
ScopedMembers :
AdditionalProperties : {[@odata.context, https://graph.microsoft.com/v1.0/$metadata#directoryRoles/$entity]}
ResponseHeaders : {edcaef71-3537-321d-9a95-715ec2ec5090}Get-MgDirectoryRoleMemberAsUser
使用例
Get-MgDirectoryRoleMemberAsUser -DirectoryRoleId 97582496-a64f-4dc5-970d-71330cc3026b | Format-List取得できる情報例
AboutMe :
AccountEnabled :
Activities :
AgeGroup :
AgreementAcceptances :
AppRoleAssignments :
AssignedLicenses :
AssignedPlans :
Authentication : Microsoft.Graph.PowerShell.Models.MicrosoftGraphAuthentication
AuthorizationInfo : Microsoft.Graph.PowerShell.Models.MicrosoftGraphAuthorizationInfo
Birthday :
BusinessPhones : {}
Calendar : Microsoft.Graph.PowerShell.Models.MicrosoftGraphCalendar
CalendarGroups :
CalendarView :
Calendars :
Chats :
City :
CompanyName :
ConsentProvidedForMinor :
ContactFolders :
Contacts :
Country :
CreatedDateTime :
CreatedObjects :
CreationType :
CustomSecurityAttributes : Microsoft.Graph.PowerShell.Models.MicrosoftGraphCustomSecurityAttributeValue
DeletedDateTime :
Department :
DeviceEnrollmentLimit :
DeviceManagementTroubleshootingEvents :
DirectReports :
DisplayName : 佐藤祐樹
Drive : Microsoft.Graph.PowerShell.Models.MicrosoftGraphDrive
Drives :
EmployeeExperience : Microsoft.Graph.PowerShell.Models.MicrosoftGraphEmployeeExperienceUser
EmployeeHireDate :
EmployeeId :
EmployeeLeaveDateTime :
EmployeeOrgData : Microsoft.Graph.PowerShell.Models.MicrosoftGraphEmployeeOrgData
EmployeeType :
Events :
Extensions :
ExternalUserState :
ExternalUserStateChangeDateTime :
FaxNumber :
FollowedSites :
GivenName : 祐樹
HireDate :
Id : ca9cff24-c787-42b9-97c7-8120f75ade90
Identities :
ImAddresses :
InferenceClassification : Microsoft.Graph.PowerShell.Models.MicrosoftGraphInferenceClassification
Insights : Microsoft.Graph.PowerShell.Models.MicrosoftGraphOfficeGraphInsights
Interests :
IsResourceAccount :
JobTitle :
JoinedTeams :
LastPasswordChangeDateTime :
LegalAgeGroupClassification :
LicenseAssignmentStates :
LicenseDetails :
Mail : y.sato@*****.onmicrosoft.com
MailFolders :
MailNickname :
MailboxSettings : Microsoft.Graph.PowerShell.Models.MicrosoftGraphMailboxSettings
ManagedAppRegistrations :
ManagedDevices :
Manager : Microsoft.Graph.PowerShell.Models.MicrosoftGraphDirectoryObject
MemberOf :
Messages :
MobilePhone :
MySite :
Oauth2PermissionGrants :
OfficeLocation :
OnPremisesDistinguishedName :
OnPremisesDomainName :
OnPremisesExtensionAttributes : Microsoft.Graph.PowerShell.Models.MicrosoftGraphOnPremisesExtensionAttributes
OnPremisesImmutableId :
OnPremisesLastSyncDateTime :
OnPremisesProvisioningErrors :
OnPremisesSamAccountName :
OnPremisesSecurityIdentifier :
OnPremisesSyncEnabled :
OnPremisesUserPrincipalName :
Onenote : Microsoft.Graph.PowerShell.Models.MicrosoftGraphOnenote
OnlineMeetings :
OtherMails :
Outlook : Microsoft.Graph.PowerShell.Models.MicrosoftGraphOutlookUser
OwnedDevices :
OwnedObjects :
PasswordPolicies :
PasswordProfile : Microsoft.Graph.PowerShell.Models.MicrosoftGraphPasswordProfile
PastProjects :
People :
PermissionGrants :
Photo : Microsoft.Graph.PowerShell.Models.MicrosoftGraphProfilePhoto
Photos :
Planner : Microsoft.Graph.PowerShell.Models.MicrosoftGraphPlannerUser
PostalCode :
PreferredDataLocation :
PreferredLanguage :
PreferredName :
Presence : Microsoft.Graph.PowerShell.Models.MicrosoftGraphPresence
Print : Microsoft.Graph.PowerShell.Models.MicrosoftGraphUserPrint
ProvisionedPlans :
ProxyAddresses :
RegisteredDevices :
Responsibilities :
Schools :
ScopedRoleMemberOf :
SecurityIdentifier :
ServiceProvisioningErrors :
Settings : Microsoft.Graph.PowerShell.Models.MicrosoftGraphUserSettings
ShowInAddressList :
SignInActivity : Microsoft.Graph.PowerShell.Models.MicrosoftGraphSignInActivity
SignInSessionsValidFromDateTime :
Skills :
State :
StreetAddress :
Surname : 佐藤
Teamwork : Microsoft.Graph.PowerShell.Models.MicrosoftGraphUserTeamwork
Todo : Microsoft.Graph.PowerShell.Models.MicrosoftGraphTodo
TransitiveMemberOf :
UsageLocation :
UserPrincipalName : y.sato@*****.onmicrosoft.com
UserType :
AdditionalProperties : {}Microsoft365管理者アカウントを一括取得する
#コマンド実行日時を取得する
$yyyyMMddFFFFF = Get-Date -Format "yyyyMMdd_FFFFF"
#CSVの保存先を指定する windowsOSの場合、デスクトップに保存する
$csvSavePath = "$env:USERPROFILE\Desktop"
#CSVのファイル名を指定する
$csvFileName = "adminUserList_${yyyyMMddFFFFF}.csv"
#取得した情報を格納する変数
$adminUserList = [PSCustomObject]@()
#GraphPowerShellに接続する
Connect-MgGraph -Scopes RoleManagement.Read.Directory -NoWelcome
#全ての役割を取得する
$roleList = Get-MgDirectoryRole -All
#役割ごとにメンバーを取得する
foreach($role in $roleList){
#役割を割り当てられているアカウント情報を取得する
$userList = Get-MgDirectoryRoleMemberAsUser -DirectoryRoleId $role.Id -All
#取得したアカウントごとに役割、UPN、表示名を格納する
foreach($user in $userList){
$adminUserList += [PSCustomObject]@{
roleName = $role.DisplayName
displayName = $user.displayName
userPrincipalName = $user.userPrincipalName
}
}
}
#取得した情報をroleName,userPrincipalNameで並び替えし
#CSVファイルとしてエクスポートする
$adminUserList | Sort-Object roleName,userPrincipalName | `
Export-Csv -Encoding UTF8 -NoTypeInformation -Path "${csvSavePath}\${csvFileName}"
Disconnect-MgGraph
問い合わせフォーム
リンク切れのご報告や、記載内容に不備がありましたら、お手数をおかけしますが
以下お問い合わせフォームからご報告をお願い致します。
コマンドの仕様確認やエラー内容の確認、対処方法については、ご契約されているMicrosoftかCSPへお問い合わせください。
※いただきました内容は確認させていただきますが、必ずしも回答をお約束するものではございません。







