Reputation: 5977
I am trying to filter/match the operating system from Get-ADComputer
and only return computers that have Windows 7 and above:
$computer = Get-ADComputer -properties OperatingSystem | Where-Object {operatingsystem -match "*Windows 7*|*Windows 8*|*Windows 10*"} |
Where-Object {$_.name -like "*-*"} |
Where-Object {$_.name -NotLike "V7-*"} |
Where-Object {$_.name -NotLike "*-NONE"} |
Where-Object {$_.name -NotLike "*-ONCALL"} |
Where-Object {$_.name -NotLike "*-BLACKBAUD"} |
Where-Object {$_.name -NotLike "SC-WIN7-1"} |
Where-Object {$_.name -NotLike "UT-SWCLIENT-01"} |
Select-Object -Expand Name
but when I do that, the debugger asks for a -Filter
parameter
I have also tried:
$computer = Get-ADComputer -properties OperatingSystem -filter {(operatingsystem -match "*Windows 7*|*Windows 8*|*Windows 10*")} |
but I get an error:
Get-ADComputer : Error parsing query: '(operatingsystem -match "*Windows 7*|*Windows 8*|*Windows 10*")' Error Message: 'Operator Not supported: -match' at position: '18'.
So what's the correct/best way to do this?
Upvotes: 0
Views: 10205
Reputation: 3350
You are trying to use multiple filters
condition with your Get-AdComputer
cmdlet. Instead of using the -match
parameter, I would suggest to use -like
parameter. I am not sure if Get-AdComputer
supports the -match
parameter. You can do something like below -
$computer = Get-ADComputer -properties OperatingSystem -filter 'operatingsystem -like "*Windows 7*" -or operatingsystem -like "*Windows 8*" -or operatingsystem -like "*Windows 10*"' |
If you see Get-Help Get-ADComputer -Examples
, you can see how the -filter
parameter is used.
Upvotes: 1
Reputation: 3421
When you are doing advanced filtering using Where-Object
you need to refer to iterator object. Also in your first code example, include -Filter *
to get a complete list of all machines. I.e. try running
$computer = Get-ADComputer -properties OperatingSystem -Filter * `
| Where-Object {$_.operatingsystem -match "*Windows 7*|*Windows 8*|*Windows 10*"}
Alternatively you can do simple filtering and exclude the $_
operator like this
Get-Process | where name -like svchost
However, the -match
operator does not seem to support this way of filtering.
You can also filtering the returned result when doing the query for all AD computers, like this
Get-ADComputer -Properties OperatingSystem `
-Filter {OperatingSystem -like "*Windows 7*" -or OperatingSystem -like "*Windows 8*"}
See this blog post on what is allowed when doing advanced filtering against Active Directory.
In my opinion, it's worth trying to do as much filtering as possible at the "server" end of the query and only return what you really need. The benefit is that all further processing at your end will be faster since there's less data to process.
Upvotes: 2