Add variable to be used with foreach to get the owner object on AD

This topic contains 5 replies, has 4 voices, and was last updated by  BGomes 2 weeks, 5 days ago.

  • Author
  • #100204


    Hello..... the sccript below is working, however I woull like to add all the information from each object in variable in order to organize the results with "| select " command. Could please somebody help me out?

    $DomainName = 'LDAP://CN=Computers,DC=london,DC=com'
    $Root = New-Object DirectoryServices.DirectoryEntry $DomainName
    $objSearcher = New-Object DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $Root
    $objSearcher.SearchScope = "SubTree"

    $colResults = $objSearcher.FindAll()
    foreach ($objResult in $colResults)

    $computer = $objResult.GetDirectoryEntry()


  • #100213

    Matt Howard

    There are a couple of ways to accomplish this. The easiest would be to create calculated properties of $computer. Calculated properties are just one-off hashtables you can use as custom or replacement properties.

    $computer = $objResult.GetDirectoryEntry()
    $computer | Select-Object -Property name,@{Name='Owner';Expression={$_.ObjectSecurity.Owner}}

    In that example, you actually would not need to create the $computer variable. You could pipe $objResult.GetDirectoryEntry() directly to Select-Object.

    • #100285


      Very appreciate your help.. so I am very beginner with powershel...How can I get the pthers proprieties that I could get from the computer? Do you advice any how to with firts steps?

      Thanks so much.

  • #100243

    Rob Simmers

    Is there a reason you are not using the ActiveDirectory module installed with RSAT tools? The code is as simple as:

    Import-Module ActiveDirectory
    $computers = Get-ADComputer -Filter * -Properties ManagedBy
  • #100251


    Also, you can't pipe from statements like "foreach($i in $list)" in powershell. That's one of the big gotchas.

You must be logged in to reply to this topic.