td;dr
$b = Get-ChildItem "E:\Export" -Filter ('*' + $a)
Or, using PowerShell's string expansion (interpolation):
$b = Get-ChildItem "E:\Export" -Filter "*$a"
-Filter
parameter values:
Since you're dealing with files, it is the FileSystem
PS provider that interprets -Filter
, and it expects a wildcard expression as an argument, as accepted by the underlying Windows API; the wildcard expression is implicitly matched against the file name.
Note:
Typically - such as in this case - such wildcard expressions work the same as PowerShell's own wildcard expressions, but the former have quirks in order to support legacy applications, while the latter offer additional features.
No standard provider accepts script blocks with arbitrary PowerShell code as a -Filter
arguments, despite their widespread - but misguided - use with the Active Directory provider - see this answer.
To perform arbitrary filtering of output objects via script blocks in PowerShell code, pipe to the Where-Object
cmdlet, as shown in LotPings' answer.
However, if feasible, use of -Filter
should always be the first choice, because it filters at the source, meaning that the provider returns the already-filtered results to PowerShell (as opposed to having to filter the results after the fact, in PowerShell code), which can greatly speed up operations.