MgGraph – User

•

# MgGraph - Connect MgGraph
Connect-MgGraph

# MgGraph - Connect MgGraph without Welcome message
Connect-MgGraph -NoWelcome

# MgGraph - Find out which permission scope is needed when using a mggraph command (in this example 'Get-MgUser')
Find-MgGraphCommand -command Get-MgUser | Select-Object -First 1 -ExpandProperty Permissions

# MgGraph - Disconnect MgGraph
Disconnect-MgGraph

# MgGraph - Search for a user
$displayName = 'John Doe'
Get-MgUser -Filter "displayName eq '$displayName'"

# MgGraph - Display all properties of a user
$displayName = 'John Doe'
Get-MgUser -Filter "displayName eq '$displayName'" | Format-List

# MgGraph - Display the UPN of a user
$displayName = 'John Doe'
Get-MgUser -Filter "displayName eq '$displayName'" | Select-Object -Property 'UserPrincipalName' 

# MgGraph - Display the UPN of a user
$displayName = 'John Doe'
(Get-MgUser -Filter "displayName eq '$displayName'").UserPrincipalName

# MgGraph - Display the user by using the UserPrincipalName
$userUPN = 'john.doe@domain.com'
Get-MgUser -UserId $userUPN 

# MgGraph - Display the Id / Object ID of a user
$displayName = 'John Doe'
Get-MgUser -Filter "displayName eq '$displayName'" | Select-Object -Property 'DisplayName','Id'

# MgGraph - List the Entra applications assigned to a user
$displayName = 'John Doe'
$userId = (Get-MgUser -Filter "displayName eq '$displayName'").Id 
$assignedApplications = Get-MgUserAppRoleAssignment -UserId $userId | Sort-Object ResourceDisplayName
$assignedApplications | Format-Table ResourceDisplayName,Id, AppRoleId, PrincipalDisplayName -AutoSize

# MgGraph - Lookup device (Intune) of a user
$displayName = 'John Doe'
$emailAddress = Get-MgUser -Filter "displayName eq '$displayName'" | Select-Object -ExpandProperty 'Mail'
Get-MgDeviceManagementManagedDevice -All -Filter "EmailAddress eq '$emailAddress'"

# MgGraph - Lookup device of a user and display all details
$displayName = 'John Doe'
$emailAddress = Get-MgUser -Filter "displayName eq '$displayName'" | Select-Object -ExpandProperty 'Mail'
Get-MgDeviceManagementManagedDevice -All -Filter "EmailAddress eq '$emailAddress'" | Format-List

# MgGraph - Lookup device of a user and display the last sync
$displayName = 'John Doe'
$emailAddress = Get-MgUser -Filter "displayName eq '$displayName'" | Select-Object -ExpandProperty 'Mail'
Get-MgDeviceManagementManagedDevice -All -Filter "EmailAddress eq '$emailAddress'" | Select-Object -ExpandProperty 'LastSyncDateTime'

# MgGraph - Lookup device(s) of a user and the last sync date
$displayName = 'John Doe'
$emailAddress = Get-MgUser -Filter "displayName eq '$displayName'" | Select-Object -ExpandProperty 'Mail'
$userDevices = Get-MgDeviceManagementManagedDevice -All -Filter "EmailAddress eq '$emailAddress'"
$loop = Foreach ($device in $userDevices) {Get-MgDeviceManagementManagedDevice -ManagedDeviceId $device.Id | Select-Object -Property 'DeviceName','LastSyncDateTime'}
$loop | Format-Table -AutoSize

# MgGraph - Lookup device(s) of a user
$displayName = 'John Doe'
$emailAddress = Get-MgUser -Filter "displayName eq '$displayName'" | Select-Object -ExpandProperty 'Mail'
$userDevices = Get-MgDeviceManagementManagedDevice -All -Filter "EmailAddress eq '$emailAddress'"
$loop = Foreach ($device in $userDevices) {Get-MgDeviceManagementManagedDevice -ManagedDeviceId $device.Id}
$loop | Format-Table -AutoSize

# MgGraph - Lookup device(s) of a user and the last sync date
$displayName = 'John Doe'
$emailAddress = Get-MgUser -Filter "displayName eq '$displayName'" | Select-Object -ExpandProperty 'Mail'
$userDevices = Get-MgDeviceManagementManagedDevice -All -Filter "EmailAddress eq '$emailAddress'"
$loop = Foreach ($device in $userDevices) {Get-MgDeviceManagementManagedDevice -ManagedDeviceId $device.Id | Select-Object -Property 'DeviceName','LastSyncDateTime'}
$loop | Format-Table -AutoSize

# MgGraph - List a user's last sign-on dates
$userDisplayName = 'John Doe'
$userId = Get-MgUser -Filter "displayName eq '$userDisplayName'" | Select-Object -ExpandProperty 'Id' 
Get-MgUser -UserId $userId -Property SignInActivity | Select-Object -ExpandProperty SignInActivity | Format-List

# MgGraph - Permanently delete a deleted Entra user
$emailAddress = 'john.doe@domain.com'
$directoryObjectId = (Get-MgDirectoryDeletedItemAsUser -Filter "Mail eq '$emailAddress'").Id
Remove-MgDirectoryDeletedItem -DirectoryObjectId $directoryObjectId

# MgGraph - Display all guest accounts
Get-MgUser -Filter "userType eq 'Guest'" -All | Format-List Id, DisplayName, Mail, UserPrincipalName

# MgGraph - Count the number of guest accounts
(Get-MgUser -Filter "userType eq 'Guest'" -All | Format-List Id, DisplayName, Mail, UserPrincipalName).count

# MgGraph - Lookup a specific quest account 
$guestEmailaddress = 'jane.doe@guest.com'
Get-MgUser -Filter "mail eq '$guestEmailaddress'" -All | Format-List Id, DisplayName, Mail, UserPrincipalName

# MgGraph - Lookup a specific quest account and display all properties
$guestEmailaddress = 'jane.doe@guest.com'
Get-MgUser -Filter "mail eq '$guestEmailaddress'" -All | Format-List 

Leave a Reply

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