Filter differences in Tick and Quotes Use (like in get-adcomputer)

PatrickJBurwell 1 Reputation point
2021-05-19T17:40:21.48+00:00

I need clarity on a syntax issue that I don't see answered anywhere:
What are the PowerShell 5+ Filter differences in Tick and Quotes use (like in get-adcomputer)?
I find that whenever I use Filter in AD commands I get a variety of differences in syntax capability.
Like in this particular syntax I see quotes (") surrounding the whole syntax with '-and' and ticks(')
for the entries are what works.

    $result = get-adcomputer -Server $LocalDomainDC -Properties * -Filter 'enabled -ne "true" -and OperatingSystem -like "*Server*"'|`
    #$result.count |`
    Export-CSV .\output\$day-ServersEnabledInActiveDirectory.csv -NoTypeInformation -Encoding UTF8 -Append

But then I see on Technet an answer marked as working with the REVERSE syntax on the Ticks and Quotes:

    get-aduser -Server $LocalDomainDC -Properties * -Filter "enabled -ne 'true' -and Name -like '*Patrick*'"

So, what are the differences here? Why do they both work? When do we know which one to use, and why?

BTW:
I set $LocalDomainDC in my logon $profile for both PS and ISE to the

$LocalDomainDC = $env:LOGONSERVER.Remove(0,2)

Name Value


PSVersion 5.1.17763.1852
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.1852
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-05-20T05:40:24.95+00:00

    Hi,

    A string enclosed in double quotation marks is an expandable string. Variable names preceded by a dollar sign ($) are replaced with the variable's value before the string is passed to the command for processing.

    A string enclosed in single-quotation marks is a verbatim string. The string is passed to the command exactly as you type it. No substitution is performed.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules

    For example, this will return a user with the name "Patrick"

    $user="patrick"  
    Get-ADUser -Filter "Name -like '$user'"  
    

    And this will return a user with the name "$user"

    Get-ADUser -Filter 'Name -like "$user"'  
    

    98151-image.png

    In your script, there are no differences for no variable is used.

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.