Identify Primary Users Azure AD PowerShell

This could make device management within the organization exceedingly challenging, especially where a large number of devices are registered in Microsoft Entra ID. There are various reasons why one may want to know who the primary user is for every device: troubleshooting issues, audits, or providing specific user support. Manually figuring out / identifying primary users of Azure AD PowerShell, the primary user for each device, is time-consuming and susceptible to errors.

Fortunately, PowerShell is a very powerful utility that helps automate these tasks seamlessly. In this blog post, we will see how to use PowerShell efficiently to find primary users of devices in Azure AD. As an IT administrator or support engineer, after reading through this guide, you will be well-equipped with the appropriate tools and knowledge you need to manage the devices in your organization productively.

Section 1: Need for Identification of Primary Users

Any organization-based device management entails much more than a guarantee of their being in running condition. The need is felt to know who is using them. There are several reasons why it is necessary to identify the primary user for each device in Azure AD:

Troubleshooting and Support: In case the device encounters problems, then the information on the primary user is important, and it gives the IT support team a starting point, which can quickly identify the person responsible for the device. That way, communication becomes much more accessible during troubleshooting, and the speed at which the problem can be solved is increased.

Security and Compliance: Knowing which user is assigned to which device is a critical record needed when security auditors come knocking. Visibility into who has which device serves as proof of compliance with company policies and regulatory requirements.

Primary User: Knowing who the primary user is for every device would let organizations provide personalized support or, more importantly, even software licensing. This information will help IT teams customize services and deploy software based on users’ needs.

Effective Device Management: Maintaining records of primary users helps IT administrators manage device life cycles effectively, efficiently use resources, and ensure that devices are used by the right people.

In this blog post, we will show you how to automate the process of identifying primary users for Azure AD devices using PowerShell.

Prerequisites

You will be using the AzureAD PowerShell module to interact with the Entra ID. If you haven’t installed this module yet, do so by running the following command in your PowerShell session:

Install-Module -Name AzureAD

Ensure you have the appropriate permissions to access device information in Azure AD. To run these commands effectively, you typically need to be a member of the Global Administrator or Intune Administrator role in Azure AD.

Connect-AzureAD
# Connect to Azure AD
Connect-AzAccount

# Import the required module (uncomment if needed)
# Install-Module -Name AzureAD
Import-Module AzureAD

# Get the Group ID for "Group300"
$group = Get-AzureADGroup -Filter "DisplayName eq 'Group300'"
$groupId = $group.ObjectId

# Get all devices in the group
$devices = Get-AzureADGroupMember -ObjectId $groupId -All $true | Where-Object {$_.ObjectType -eq "Device"}

# Iterate over each device and get the primary user
$devicePrimaryUsers = @()

foreach ($device in $devices) {
    $deviceId = $device.ObjectId
    $primaryUser = Get-AzureADDeviceRegisteredOwner -ObjectId $deviceId | Where-Object {$_.ObjectType -eq "User"}

    if ($primaryUser) {
        $devicePrimaryUsers += [PSCustomObject]@{
            DeviceName  = $device.DisplayName
            PrimaryUser = $primaryUser.UserPrincipalName
        }
    } else {
        $devicePrimaryUsers += [PSCustomObject]@{
            DeviceName  = $device.DisplayName
            PrimaryUser = "No Primary User Found"
        }
    }
}

# Output the results
$devicePrimaryUsers | Format-Table -AutoSize

# Optionally export to CSV
$devicePrimaryUsers | Export-Csv -Path "DevicePrimaryUsers.csv" -NoTypeInformation

Leave a Comment