Retreiving ADComputer Obj based on filter...

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
cody m
Site Admin
Posts: 123
Joined: Thu Jun 01, 2017 7:20 am

Re: Retreiving ADComputer Obj based on filter...

Post by cody m » Wed Nov 01, 2017 8:22 am

So you're almost right with using -notlike to get everything that is not Windows 2008, and Windows 2012; but you need to use -and rather than -or so it would look like this.

Code: Select all

foreach ($Machine in $Servers) {
$UnUseableOS += get-adcomputer -Identity $Machine -Properties * | Select-Object -property name,operatingsystem | where-object -filterscript { $PSItem.operatingsystem -notlike "Windows Server 2008*" -and $PSItem.operatingsystem -notlike "Windows Server 2012*" }
Cody McCants
SAPIEN Technologies, Inc.

User avatar
Posts: 12774
Joined: Tue May 22, 2007 9:57 am

Re: Retreiving ADComputer Obj based on filter...

Post by jvierra » Wed Nov 01, 2017 9:23 am

You don't need to or want to use select-object:

Code: Select all

$servers |
    ForEach-Object{get-adcomputer $_ -Properties operatingsystem} |
    Where-Object{$_.operatingsystem -notmatch 'Windows Server 2008|Windows Server 2012' }|
    Select-Object name, operatingsystem
Selecting and formatting should always be done last. Using "match" is more flexible and can coalesce logic statements for simplicity and readability.