Collect device information against User Groups

During operations we get requirement where we need to extract devices for specific group members, In order to achieve this i checked dynamic device groups and filters but till now there is no option to extract this information.

To extract this information we first needs to extract group members, then for each member we needs to look for device information, There is possibility of more than one devices registered. So we need another loop to extract devices information. At the end add results in the collection defined in start of the script. In this case $ExpectedResults is the one. Lastly we can show it within PowerShell windows or can extract into CSV for further analysis.

# Connect with Azure AD
Connect-AzureAD
# Initialize Collection
$ExpectedResults = @()

# Collect Group members, Add Group Object ID below
$GroupMembers = Get-AzureADGroup -ObjectId "Group Object ID" | Get-AzureADGroupMember | Select UserPrincipalName,ObjectId

$GroupMembers | ForEach-Object 
{
    $user = $_
    Get-AzureADUserRegisteredDevice -ObjectId $user.ObjectId | ForEach-Object 
    {
        $ExpectedResults += New-Object PSObject -property @{ 
            OwnerEmail = $user.UserPrincipalName
            DeviceName = $_.DisplayName
            OSType = $_.OSType
            LastLoginTimeDate = $_.LastLoginTimeDate
        }
    }
}

$ExpectedResults | Select OwnerEmail, DeviceName, OSType,LastLoginTimeDate 

In case you want to extract this information in CSV, replace last line with below mentioned command

$ExpectedResults | Export-Csv "C:\results\group_users_devices.csv"

Leave a comment