Powershell: Fill AD computer description with location data

Musicmaker 5 Reputation points
2023-05-02T09:57:29.28+00:00

Hi,

I want to fill the description field for computers in a specific AD OU with information from the 'location' tab.

For now, this is what I have:

$computers = Get-ADComputer -Filter * -SearchBase "OU=Blabla,DC=BLABLA" -Properties name | select name
foreach ($computer in $computers)
{$ADlocation = (Get-ADComputer -Filter * -SearchBase "OU=Blabla,DC=BLABLA" -Properties location | select -ExpandProperty location)
Set-ADComputer -Identity $computer –Description “$ADLocation”
}

At this point, the script runs but nothing happens in AD. I don't get any errors.
Is this the right way to define the variable for the location property? What else is wrong here? Hope someone can help me out.

Windows for business | Windows Client for IT Pros | Directory services | Active Directory
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 44,751 Reputation points
    2023-05-03T14:05:56.91+00:00

    Hello,

    If the programatically way fails, you can try to run manually to see if any error appears during the input of data.

    For example run:

    Get-ADComputer <hostname> -Properties location | select -ExpandProperty location

    then

    Set-ADComputer <hostname> –Description “<manual input of location>”

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2023-05-03T14:51:39.4033333+00:00

    Try this:

    Get-ADComputer -Filter * -SearchBase "OU=Blabla,DC=BLABLA" -Properties name,location,description |
        ForEach-Object  {
                Set-ADComputer -Identity $_.distinguishedName -Description $_.location
        }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.