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.

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

2 answers

Sort by: Most helpful
  1. Limitless Technology 44,391 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 46,811 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.