Try the following:
$computers = Get-Content f:\computer-list.txt
$results = foreach ($computer in $computers) {
$device = Get-AzureADDevice | Where-Object { $_.DisplayName -eq $computer }
$owner = Get-AzureADDeviceRegisteredOwner -ObjectId $device.ObjectId
[PSCustomObject]@{
ComputerName = $computer
OwnerName = $owner.DisplayName
OwnerUserPrincipalName = $owner.UserPrincipalName
}
}
$results | Export-CSV f:\ComputerUsers.csv -NoTypeInformation
In this script:
- We create a custom object
[PSCustomObject]
that includes properties forComputerName
,OwnerName
, andOwnerUserPrincipalName
. - We use
Get-AzureADDeviceRegisteredOwner
to retrieve the owner information for each device. - We then export the results to a CSV file using
Export-CSV
. The-NoTypeInformation
parameter is used to exclude the type information from the CSV file.
This should give you a CSV file with both the computer name and the owner's name included.
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