Quering AD to get Windows Server List


I’m doing a basic query to find out the all the Server in one domain. I run the following line

Get-ADComputer -Filter {OperatingSystem -Like “Windows Server*”} -Property * | Format-Table Name,OperatingSystem,OperatingSystemServicePack -Wrap -Auto

and I’m getting this error

Get-ADComputer : Error parsing query: ‘OperatingSystem -Like “Windows Server*”’ Error Message: ‘syntax error’ at position: ‘23’.
At C:\Users\hguzman\Desktop\PSScripts\servers.ps1:10 char:1

  • Get-ADComputer -Filter {OperatingSystem -Like “Windows Server*”} -Property * | F …
  •   + CategoryInfo          : ParserError: (:) [Get-ADComputer], ADFilterParsingException
      + FullyQualifiedErrorId : Error parsing query: 'OperatingSystem -Like “Windows Server*”' Error Message: 'syntax error' at position: '23'.,Microsoft.Act 

why I get this error?

looks like powershell doesn’t like your quotes in the filter , try this

Get-ADComputer -Filter {OperatingSystem -Like "Windows Server*"} -Property * | Format-Table Name,OperatingSystem,OperatingSystemServicePack -Wrap -Auto

There is something wrong with the open quotes. I can’t tell visually, but if I replace it, it works.

Get-ADComputer -Filter {OperatingSystem -Like “Windows Server*”} -Property * | Format-Table

This works fine
Get-ADComputer -Filter {OperatingSystem -like “Windows Server*”} -Properties *

Looks like its the quotes rather than the syntax

Single quotes work as well

That usually happens when you copy code from a website or a word document. Just replace the curved quotation marks with the normal straight ones and I assume it will work as expected.