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

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()


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.

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

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


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.