• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6103
  • Last Modified:

Ad Computer Search in AD excluding one OU only

Hello Powershell Junkies,

I'm looking for simplicity here and I have an easy query that I cant get a grasp on. I'm trying to run a very simple script to retrieve machine names that start with a certain few letters, the search is very simple, search all of AD except for our disabled workstations OU.  Can I have all the filters for different machine names in one line of code? and how do I ommit the OU? or should I add another filter to remove Machines that are disabled? I also want to export this to CSV  ( Have the Powershell active directory role installed)

Here's some of what I've tried and dont work (as you can tell, I am a noob)

Get-ADComputer -filter Name -like "Cow*" -filter Name -like "Col"{$_.parentcontainer -ne "*disabledworkstations*"}

Get-ADComputer -Filter 'SamAccountName -like "Cow*"' -Filter 'SamAccountName -like "Col*"' -notlike "*ou=disabledworkstation*"  | Select-Object Name | Export-csv u:\Machines.csv

Sorry I know this is a terrible effort on my behalf, but I'd like to learn the best way to do this. I dont mind having to filter machine names in a seperate script.
  • 3
  • 2
1 Solution
Get-ADComputer -filter {(enabled -eq $True) -and ((name -like "Cow*") -or (name -like "Col*"))} | Select name > U:\Machines.txt
This will not show any disabled computers, so you don't have to exclude that OU by name. There is no need to export to a csv files since you have only one column of Names. A simple text files is just as good.
Get-ADComputer -filter {(enabled -eq $True) -and ((name -like "Cow*") -or (name -like "Col*"))} | Select name |export-csv U:\Machines.csv
Imraz_AAuthor Commented:
This is Brilliant! it works, but can you please tell me how I can work with OU's? filtering them etc? incase there was a quirie I needed to use to exclude one or more OU's?  thank you again!
You can insert a '|Where' -notlike, with an asterisk and OU's distinguished name:
{$_.DistinguishedName -notlike "*OU=Disabledworkstation,dc=YourDomain,dc=com"}

So the total command becomes:
Get-ADComputer -filter {(enabled -eq $True) -and ((name -like "Cow*") -or (name -like "Col*"))} | Where {$_.DistinguishedName -notlike "*OU=Disabledworkstation,dc=YourDomain,dc=com"}| Select name > U:\Machines.txt
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Just suggesting a different approach to solve the problem:

1. Go to View menu in the Active Directory. Click on Find Computers dialog box. Click Choose Columns.

2. Columns available box will display list of all columns that you can have. Select Published At column and click Add.

3. Click Ok.

Now, when you run this function, the search result will also include the parent OU for found computers. In that way you can make out what all computers are in your disabled computers’ OU.

But this will only show you the parent OU information; it will not filter out items from disabled computers OU.
In my previous post, if all disabled computers are in that excluded OU, you can remove:
(enabled -eq $True) -and
from the command and still get the same results. (May have to remove superfluous brackets.)
Imraz_AAuthor Commented:
Great asnwers all, just waht I was looking for, I was specifically trying to find Powershell Solutions.

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now