Bill
Bill

Reputation: 554

How can I exclude particular names from Get-ADComputer results?

I want to get all computers in my domain that are enabled, and have 2003 operating system, and the name of the computers do Not contain ' ping , pict , pire ' Here is what I have, but totally failing:

Get-ADComputer -filter {(Enabled -eq $True) -and (OperatingSystem -like "*2003*")} -properties OperatingSystem | where {($_.Name -notlike 'PING*') -or ($_.Name -notlike 'PICT*') -or ($_.Name -notlike 'PIRE*')} | Select Name

Upvotes: 2

Views: 11546

Answers (1)

xXhRQ8sD2L7Z
xXhRQ8sD2L7Z

Reputation: 1716

You can use the -notlike operator inside the filter, so there is no need for the where statement. See the Get-ADComputer reference on technet.

As well as changing your -or operators to -and as I mentioned, I put all conditions into the filter ending up with this:

Get-ADComputer -filter {
   Enabled -eq $True -and
   OperatingSystem -like '*2003*' -and
   Name -notlike 'PING*' -and
   Name -notlike 'PICT*' -and
   Name -notlike 'PIRE*'
} | Select Name

Upvotes: 5

Related Questions