Get device ObjectID from a CSV list of computers using PowerShell

Ahmed Alshatawi 60 Reputation points
2024-09-20T20:11:35.87+00:00

Hello,

I have a list of 400 with device name and need to upload these to a Azure AD group. However, import bulk members required ObjectID. I need a script which will take the device name from the file containing the device and get the ObjectIDs of these devices please and thank you!

Microsoft Intune
Microsoft Intune
A Microsoft cloud-based management solution that offers mobile device management, mobile application management, and PC management capabilities.
4,996 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
21,469 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Marcin Policht 22,455 Reputation points MVP
    2024-09-20T20:55:41.36+00:00
    1. Prepare the CSV file

    Make sure your file (e.g., devices.csv) has a single column with the header DeviceName:

    DeviceName
    Device1
    Device2
    Device3
    ...
    
    1. PowerShell script

    This script will read the device names from the CSV file, fetch the corresponding ObjectIDs, and then output the results to another CSV file.

    # Path to the input file containing device names
    $inputFilePath = "C:\path\to\devices.csv"
    
    # Path to the output file that will store the results with ObjectIDs
    $outputFilePath = "C:\path\to\deviceObjectIDs.csv"
    
    # Import the CSV file containing device names
    $devices = Import-Csv -Path $inputFilePath
    
    # Create an empty array to hold the results
    $results = @()
    
    foreach ($device in $devices) {
        # Fetch the device object from Azure AD using the device name
        $deviceObject = Get-AzureADDevice -All $true | Where-Object { $_.DisplayName -eq $device.DeviceName }
    
        if ($deviceObject) {
            # If the device is found, create a custom object with DeviceName and ObjectID
            $results += [PSCustomObject]@{
                DeviceName = $device.DeviceName
                ObjectID = $deviceObject.ObjectId
            }
        } else {
            # If the device is not found, log it (optional)
            Write-Warning "Device $($device.DeviceName) not found in Azure AD."
        }
    }
    
    # Export the results to a CSV file
    $results | Export-Csv -Path $outputFilePath -NoTypeInformation
    
    Write-Host "Device ObjectIDs have been saved to $outputFilePath"
    
    
    1. Running the Script

    Make sure that you have the AzureAD module installed

    Install-Module -Name AzureAD 
    
    

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.