@Ahmed Alshatawi,Thanks for posting in Q&A.
Based on my research, here is script temple you can refer.
Install-Module -Name AzureAD
Connect-AzureAD
$computers = Import-Csv -Path "your csv file path"
$objectids = foreach ($computer in $computers) { Get-AzureADDevice -All $true -SearchString "$($computer.userprincipalName)" | select ObjectID }
$objectids | Export-csv -Path "output csv file path" -NoTypeInformation
In the command at line ($objectids
), make sure the userprincipalName matches the column heading in your CSV file
Or based on my research, here are some links with useful methods you can refer.
https://stackoverflow.com/questions/69835775/in-powershell-get-upn-from-pc-name
Non-official, just for reference.
Hope above information can help you.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.