Search code examples
azurecsvexport-to-csvobjectid

Exported .CSV files comes up empty


I need a quick script to convert device names to Object IDs, so that I can perform a bulk upload in Intune. I have the device names saved as a .csv which I import. After running the script the output BulkObjectID.csv comes up empty (0 kb). I am not sure what I could be doing wrong. Thanks in advance for any help.

connect-azuread

$csv = Import-Csv C:\Tools\NEW.csv
$ObjectID=@()
foreach ($DisplayName in $csv){
    $ObjectID += get-AzureADDevice -Filter "DisplayName eq '$._DisplayName'" | Select ObjectID 
}
$ObjectID
$ObjectID | export-csv -path 'C:\Tools\BulkObjectID.csv' -append

Solution

  • I tried to reproduce the same in my environment and got below results

    I have few Azure AD devices existing in my tenant like below:

    enter image description here

    I created one csv file with display names of above devices like this:

    enter image description here

    Now, I ran the same script as you and got same output with empty (0 kb) BulkObjectID.csv file like below:

    Connect-AzureAD
    
    $csv = Import-Csv C:\test\new.csv
    $ObjectID=@()
    foreach ($DisplayName in $csv){
        $ObjectID += get-AzureADDevice -Filter "DisplayName eq '$._DisplayName'" | Select ObjectID 
    }
    $ObjectID
    $ObjectID | export-csv -path 'C:\test\BulkObjectID.csv' -append
    

    Response:

    enter image description here

    When I checked the folder, empty (0 kb) BulkObjectID.csv file is present like below:

    enter image description here

    To resolve this, modify your script by making few changes like below:

    Connect-AzureAD
    
    $csv = Import-Csv C:\test\new.csv
    $ObjectID=@()
    foreach ($DisplayName in $csv)
    {
        $name = $DisplayName.DisplayName
        $ObjectID = get-AzureADDevice -Filter "DisplayName eq '$name'" | Select ObjectID
        $ObjectID
        $ObjectID | export-csv -path 'C:\test\ObjectID.csv' -append
    }
    

    Response:

    enter image description here

    When I checked the folder, ObjectID.csv file is present with device IDs like below:

    enter image description here