PowerShell Function to return Computer objects nested within a base OU

I needed to gather the system volume sizes for all the servers on our estate as part of a scoping exercise for a Microsoft DPM project. It seemed to me that WMI was probably the answer, and a brief Bing search discovered this script.

However, I didn’t want to manually type out a text file to feed it computer names. Nor do I trust our CMDB to be up to date enough that I could use that as a reference point. Therefore, I used the best CMDB in our org, Active Directory. One brief coding scripting session resulted in the PowerShell function below, which will recurse though OU’s nested below a base OU, specified by it’s LDAP path in the initial function call and captures all discovered computer object name values to a string object. It can be easily modified to capture whatever property you are interested in, so feel free to use & abuse. Enjoy!

Function GetCompsToString

{param ($baseou)

$ou = [ADSI] “$baseou”

foreach ($child in $ou.psbase.Children )


if ($child.ObjectCategory -like ‘*computer*’) {$strComputer +=$child.Name}

Elseif ($child.ObjectCategory -like ‘*Organizational-Unit*’) {GetCompsToString $child.path}


return $strComputer



$output = GetCompsToString $input

Thanks to Joachimm Naslander (http://social.technet.microsoft.com/Forums/en/winserverManagement/thread/a0cebabf-3fc9-49b0-be55-5e4ff3232b7b) for the core method I used.

NOTE!: I aim to use some more easily parsed way to publish code, so this will be retroactively enhanced.

Leave a Reply