Skip to content

Get-AdUser's Filter Parameter; Guidelines on curly braces vs string. #3166

Open
@JohnLBevan

Description

@JohnLBevan

The Filter parameter accepts an argument of type string; but can also take a filter formatted between curly braces like a script block.

Examples show both formats, and there's a helpful description of the differences around quoting variables when interpolating in a string filter vs providing within curly braces: https://learn.microsoft.com/en-us/powershell/module/activedirectory/get-aduser?view=windowsserver2022-ps#-filter

However, there's no guidance on when to use which approach. Is this pure personal preference, or are there considerations?
i.e. Does using curly braces mean that you're protected from issues around character escaping / date format issues / etc?
Conversley what are the benefits of using strings; e.g. simpler to understand espacially when creating string filters or working with fixed values)?

There's some great info in this SO Answer; but this focusses primarily on string interpolation vs leaving variable references within strings, with just a side note on the "scriptblock" approach.

Additionally, is it best to use variable references vs resolving those variables to values?

Example scenarios:

The below examples help clarify the benefits of some approaches vs others; but there's little apparant difference between the single quotes vs curly braces approaches.

$name = "Chris O'Dowd"
Get-AdUser -Filter 'Name -eq $name' #automatically handles the character escaping
Get-AdUser -Filter {Name -eq $name} #automatically handles the character escaping
# Get-AdUser -Filter "Name -eq $name" # would error as this is invalid syntax
# Get-AdUser -Filter "Name -eq '$name'" # would error as the value is not escaped
Get-AdUser -Filter "Name -eq '$($name -replace '''', '''''')'" #we have to escape the offending quote; so this is inferior

$when = [DateTime]'2022-01-12' # on a US system this would be 01/12/2022; on most EU systems this would be 12/01/2022
Get-AdUser -Filter 'Created -ge $when' # automatically compares dates as expected
Get-AdUser -Filter {Created -ge $when} # automatically compares dates as expected
# Get-AdUser -Filter "Created -ge $when" # would error as invalid syntax
Get-AdUser -Filter "Created -ge '$when'" # unpredictable; could give you all users created after 12th Jan, or could give you all after 1st Dec.
Get-AdUser -Filter ("Created -ge '{0:yyyy-MM-dd}'" -f $when) # formats the date in a less ambiguous way... but theoretically could be misinterpreted if anyone uses the yyyy-dd-MM format.

# also interesting - this gives visibility of what's being sent as the filter ... but the date format may be different in the string interpolation vs when the date's output directly!
write-verbose "Created -ge '$when'" -Verbose
write-verbose $when -Verbose

Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

Labels

area-activedirectoryIssues for activedirectory moduleissue-doc-bugSomething is out of date, unclear, confusing, or broken in the article. Blocks customer success.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions