Powershell: Fill AD computer description with location data

Musicmaker 5 Reputation points


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.

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,106 questions
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,241 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 44,081 Reputation points


    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


    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 45,431 Reputation points

    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