3 minutes reading time (541 words)
Featured

Get Azure AD device object ID from computer display name using PowerShell

How to get Azure AD device object ID from computer display name using PowerShell and export to CSV file. Then use the CSV file to bulk import members to an Azure AD group.

Get computer names from Active Directory

Recently, I had to add a number of computers to an Azure AD security group to use for testing an Intune app deployment. The bulk import group members option that Microsoft provides in the Azure portal uses a CSV template that requires the device object ID.

So how do I get the device object ID for 100+ computers from their display name using PowerShell?

To start with, I exported a list of computer names from AD Users and computers.

Right click on the OU containing the computer objects - Export list

Bulk import group members using the Azure Portal

Azure AD - Groups
Select group - members

Import members

Download CSV template

You can't use the computer display name in the CSV template, it needs the device object ID

You can use the following PowerShell to get device object IDs from computer display names to bulk import members to an Azure AD group

Get Azure AD device ObjectID from DisplayName using PowerShell Get-AzureADDevice

Get device ObjectId for one computer using PowerShell Get-AzureADDevice 

# Connect to Azure AD
Connect-AzureAD

# Define variable for computer name
$ComputerName = "PELXXASJVFK8ZX"

# Get AAD device properties, format list
Get-AzureADDevice -SearchString $ComputerName | fl

# Get AAD device attributes, select DisplayName and ObjectId
Get-AzureADDevice -SearchString $ComputerName | select DisplayName,ObjectId 

Get device ObjectId for all Windows computers using PowerShell Get-AzureADDevice

# Connect to Azure AD
Connect-AzureAD

# Get device ObjectId for all Windows computers
Get-AzureADDevice -Filter "startswith(DeviceOSType,'Windows')" | select DisplayName,ObjectId 

Get device ObjectID from a CSV list of computers using PowerShell Get-AzureADDevice

The CSV file im using has just one column "Name" with all the computer display names

# Connect to Azure AD
Connect-AzureAD

# Get device ojbect id from computer display name using a CSV list
$computers = Import-Csv -Path "C:\temp\computer-list.csv"  
foreach ($computer in $computers) {  
Get-AzureADDevice -SearchString "$($computer.name)" | select DisplayName,ObjectID }

# Get device ojbect id from computer display name using a CSV list
$computers = Import-Csv -Path "C:\temp\computer-list.csv"  
$objectids = foreach ($computer in $computers) {  
Get-AzureADDevice -SearchString "$($computer.name)" | select ObjectID }
# Export device object IDs to CSV
$objectids | Export-csv -Path "C:\temp\computer-list-objectids.csv"  

Bulk update AAD group membership from CSV using the Azure Portal

Delete the example line from the CSV template

Copy the device object ids into the template

Azure AD - Groups
Select group - members - Import members

Browse to select the CSV file - click submit

File uploaded sucessfully

Bulk import completed

Object IDs imported successfully

Reference:

Azure PowerShell Get-AzureADDevice
https://learn.microsoft.com/en-us/powershell/module/azuread/get-azureaddevice

Related Posts

 

Comments

No comments made yet. Be the first to submit a comment
Already Registered? Login Here
Saturday, 23 September 2023
You can help support this website by buying me a coffee!
Buy Me A Coffee