2

I'm having trouble using backtick (grave accent) for multi-line filter expression in PowerShell with the Get-AdUser command. Specifically I'm testing PowerShell 2.0 on Windows 7.

A working example (filter is all on one line):

Get-ADUser -Filter {Name -like "Smith*" -and Enabled -eq $True}
# This works as expected, a list of matching objects is returned on pipeline

A broken example (filter is split with multiple lines):

Get-ADUser -Filter {Name -like "Smith*" `
-and Enabled -eq $True}
# Error message indicates "Operator not supported at position [of backtick]

A second broken example (multi-line filter with -and before backtick):

Get-ADUser -Filter {Name -like "Smith*" -and `
Enabled -eq $True}
#Error message indicates "Syntax error at position [of backtick]"

Please provide examples of workarounds or explain the reason that these multi-line filters aren't supported. I'm having trouble figuring this one out but it seems like this should be a simple and common type of operation in PowerShell.

Mister_Tom
  • 1,500
  • 1
  • 23
  • 36

2 Answers2

4

I am sure someone more experienced can help with this one but the backtick does not appear to be consumed by PowerShell for this line so it is making it to the AD Filter parser which does not like it. I did not find anything in about_ActiveDirectory_Filter that would explain this exactly. Backtick is still used to escape characters though. Likely the newline is being passed to the AD Filter parser and it does not like it.

The workaround I am aware of that works is to build your scriptblock into a variable before hand and pass that to Get-Aduser

$sb = {
    Name -like "Matt*" -and
    Enabled -eq $True
}
Get-ADUser -Filter $sb
Matt
  • 45,022
  • 8
  • 78
  • 119
  • Hello @Matt, this helped me get an acceptable workaround. I didn't have to create a separate script block though. It was sufficient to just end each line with -and which keeps the parser looking for more (I think?). It won't let me put multi-line code in a comment - trying to figure out where to put it ... – Mister_Tom Aug 17 '17 at 17:02
1

Posting multi-line code sample inspired by @Matt's answer. It was sufficient to just end each line with -and (removing back-ticks). The -and seems to keep the parser looking for more. Here's a working example based on his answer:

Get-ADUser -Filter {Name -like "Smith*" -and
Enabled -eq $True}
# Works as expected, drop backtick and end each line with -and (or similar)

Strange behavior with the filter and script block parser, but this workaround is acceptable to me. To confirm this seems to just be related to -Filter option of trouble command, see the following example using straight powershell with script block (back-tick multi-line escape works).

& {1 -eq 1 `
-and 2 -eq 2}
# This works as expected, output is "True"
Mister_Tom
  • 1,500
  • 1
  • 23
  • 36
  • I feel silly now since this is just how basic PowerShell parser works. Surprised it never occurred to me to just omit the backtick since it is usually not required for most multi-line comments. – Matt Aug 17 '17 at 17:13
  • This has tripped me up in the past and I never remember it so now we have it for all eternity in an SO QA post ;-). Thanks for helping me work this out! Next time Google will find this post for me I'm sure. – Mister_Tom Aug 17 '17 at 17:15