- Prepare the CSV file
Make sure your file (e.g., devices.csv
) has a single column with the header DeviceName
:
DeviceName
Device1
Device2
Device3
...
- 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"
- 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