Ad Computer Search in AD excluding one OU only

Posted on 2012-08-29
2 Endorsements
Last Modified: 2012-09-17
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.
Question by:Imraz_A
    LVL 3

    Expert Comment

    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

    Author Comment

    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!
    LVL 3

    Accepted Solution

    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
    LVL 5

    Expert Comment

    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.
    LVL 3

    Expert Comment

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

    Author Comment

    Great asnwers all, just waht I was looking for, I was specifically trying to find Powershell Solutions.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
    Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
    Viewers will learn the different options available in the Backstage view in Excel 2013.
    The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now