Link to home
Start Free TrialLog in
Avatar of Imraz_A
Imraz_A

asked on

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.
Avatar of Akulsh
Akulsh
Flag of India image

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.
Otherwise
Get-ADComputer -filter {(enabled -eq $True) -and ((name -like "Cow*") -or (name -like "Col*"))} | Select name |export-csv U:\Machines.csv
Avatar of Imraz_A
Imraz_A

ASKER

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!
ASKER CERTIFIED SOLUTION
Avatar of Akulsh
Akulsh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.)
Avatar of Imraz_A

ASKER

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