Variable in the file’s name filter.

Hello!

I am new to PowerShell scripting your help will be really appreciated!

The following works for me by running it as .ps1 script:

$source="C:\Backup\Full\"
Get-ChildItem $source -Recurse -filter "aaa_bbb_client_FULL.txt"

The following does not work for me by running it as .ps1 script:

$client="sclient"
$source="C:\Backup\Full\"
Get-ChildItem $source -Recurse -filter "aaa_bbb_" + $client + "_FULL.txt"

error I am getting:
Get-ChildItem : A positional parameter cannot be found that accepts argument '+
'.

Please help me to understand what I am missing.

Regards

well, you’ve put “sclient” into the second example, whereas the first used “client”. But, you don’t need to concatenate:

Get-ChildItem $source -Recurse -filter “aaa_bbb_$($client)_FULL.txt”

Ought to give the same result.

Try
Get-ChildItem $source -Recurse -filter “aaa_bbb_$client_FULL.txt”

or

Get-ChildItem $source -Recurse -filter (“aaa_bbb_” + $client + “_FULL.txt”)

Filter expects a single string to define the filter criteria. Using + (concatenation) it only looks at the first string and treats the rest as different parameters

Thank you so much for your help!!! It worked.

PS

Sorry for typo

The following worked!!!

Get-ChildItem $source -Recurse -filter “aaa_bbb_$($client)_FULL.txt”