Author Posts

April 10, 2014 at 9:29 am

Getting all PCs in an OU. Need to exclude a list of SubOUs.
I can exclude single OU with "Where{$_.parentContainer -ne 'mydomain/OU/servers'}

Any idea how can i exclude a list of OUs from a text file ?

April 10, 2014 at 11:33 am

Hi,

I am also at my beginnings with Powershell.
I don't say that my solution is the best, but at least it's an idea how to do it.
I have no idea if you are looking for a cmd-let or a solution.
If you just look for a solution, you can do this:

1. Get all PCs from all OUs and store them in a variable $a.
2. Then get all PCs from the OUs which you want to exclude later on (use foreach) and store them in another variable $b.
Then use:

(Compare-Object $a $b).Inputobject

The contents of this comparison will be the PCs which are of interest of you 🙂

Please come back if you need some more detailed code.

Best regards,
Andrei Luca

April 10, 2014 at 5:00 pm

I assume that you're using the Quest AD cmdlets. I'm not familiar with a "ParentContainer" property on any of the Microsoft ADSI classes or ActiveDirectory module objects. I'm not sure if there's a better, "filter on the left" approach here, but assuming the PowerShell syntax you've posted works for a single OU, then you can definitely do this:

$ousToExclude = @(Get-Content -Path .\excludedOUs.txt)

Get-QADUser @params |
Where-Object { $ousToExclude -notcontains $_.ParentContainer }

I don't know what parameters you're using to call Get-QADUser, so I just put "@params" there. The main point is to show how to load up a text file into an array of strings, then use the -notcontains parameter to test whether the ParentContainer string is found in the file. (If you're using PowerShell 3.0 or later, you can also do Where-Object { $_.ParentContainer -notin $ousToExclude } , and the result would be the same.)