Get device ObjectID from a CSV list of computers using PowerShell

Ahmed Alshatawi 100 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.
5,109 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,902 questions
0 comments No comments
{count} votes

Accepted answer
  1. Raja Pothuraju 6,825 Reputation points Microsoft Vendor
    2024-09-24T15:02:08.7266667+00:00

    Hello @Ahmed Alshatawi,

    Thank you for posting your query on Microsoft Q&A.

    To easily retrieve a list of all device names and their Object IDs, you can run the following command:

    Install-Module -Name AzureAD 
    Connect-AzureAD
    Get-AzureADDevice -All $true | select DisplayName,ObjectId | Export-Csv -Path C:\Users\<username>\Documents\devices.csv -NoTypeInformation
    

    Note: Please change the file path before running the command.

    After running the command, the output will be saved in a CSV file with the following columns:

    • DisplayName
    • ObjectId

    User's image

    I hope this information is helpful. Please feel free to reach out if you have any further questions.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Thanks,
    Raja Pothuraju.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Marcin Policht 24,630 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.