question

PatrickBurwell-7470 avatar image
0 Votes"
PatrickBurwell-7470 asked IanXue-MSFT answered

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

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-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

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://docs.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.



image.png (91.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.