How can I export data to see the owner of the workstation from a list of pc's from a csv file.

Javier Sanchez 0 Reputation points
2024-03-28T17:17:05.11+00:00

This works fine for me but the output does not list the computer name. I wasn't sure how to add that to the command to include the computer name along with the owner's name.

$computers = Get-Content f:\computer-list.txt
$results = foreach ( $computer in $computers ) {
PowerShell

    $device = Get-AzureADDevice | where { $_.DisplayName -eq $computer }

    $devName = Get-AzureADDevice -SearchString $computer | Select -ExpandProperty DisplayName

    write-host $devName

    Get-AzureADDeviceRegisteredOwner -ObjectId $device.ObjectId | Select-Object UserPrincipalName,DisplayName        

           }

     



     $results | Export-CSV f:\ComputerUsers.csv 
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,058 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Marcin Policht 10,675 Reputation points MVP
    2024-03-28T17:46:43.7533333+00:00

    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 for ComputerName, OwnerName, and OwnerUserPrincipalName.
    • 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


  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,731 Reputation points Microsoft Vendor
    2024-03-29T02:38:06.2466667+00:00

    Hi Javier Sanchez,

    You only need to get the devices once.

    $computers = Get-Content f:\computer-list.txt
    Get-AzureADDevice | Where-Object { $_.DisplayName -in $computers } | ForEach-Object { 
      Get-AzureADDeviceRegisteredOwner -ObjectId $_.ObjectId |
      Add-Member -Name "DeviceName" -Value $_.DisplayName -MemberType NoteProperty -PassThru |
      Select-Object UserPrincipalName,DisplayName,DeviceName
     } | Export-CSV f:\ComputerUsers.csv
    

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    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.

    0 comments No comments