Get username from custom computer attribute and add location from user object

Martin Chabr 21 Reputation points

I need a single powershell script combining ad-computer + get-user to get the missing user attribute. I dont now how to combine the two classed into one script.
The Computer object attribute: "extensionattribute1" already has the Username in it, and i need to add the "physicalDeliveryOfficeName" to the computer attrbiute:

Something like:

get-adcomputer COMPUTER1 -Properties Name,extensionattribute1 | select Name,extensionattribute1,(IN HERE I WOULD NEED THE QUERY FOR USER ATTRIBUTE "physicalDeliveryOfficeName")

set-ADComputer -Replace @{extensionAttribute2 = "$($_.physicalDeliveryOfficeName)"}

I have seen this for the Manager attribute. I would need the "physicalDeliveryOfficeName":
@{n='Manager';e={(Get-ADUser $_.Manager).Name}}

I need to combine these parts together into one script so i can write the location into the empty computer attribute extensionattribute2.

Computer Object: NOW:

PC1,John Doe,"NOT SET"


PC1,John Doe,Office location xyz

Thanks in advance

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,449 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,671 Reputation points

    A bit longer than one line, but it does have error checking:
    EDIT: Moved the $n = $_.Name inside the ForEach-Object block.

    Get-ADComputer COMPUTER1 -Properties Name, extensionattribute1 |
            $n = $_.Name  # needed to report computer name in Catch block
            $u = Get-ADUser -Filter {Name -eq $_.extensionattribute1} -Properties physicalDeliveryOfficeName
            if ($u){
                    $p = $u.physicalDeliveryOfficeName
                    Set-ADComputer $_ -Replace @{extensionattribute2 = $p} -ErrorAction Stop
                    Write-Host "Failed to update computer object '$n'"
                Write-Host "Failed to find user '$($_.extensionattribute1)"

1 additional answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 33,951 Reputation points Microsoft Vendor


    You can try something like below

    $computers = 'computer1','computer2'  
    $computers | Get-ADComputer -Properties extensionattribute1 | ForEach-Object{  
        $office = (Get-ADUser -Filter {Name -eq $_.extensionattribute1} -Properties physicalDeliveryOfficeName).physicalDeliveryOfficeName  
        Set-ADComputer -Identity $_ -Replace @{extensionattribute2 = $office}  

    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.