I put together some PowerShell code to search for PDFs where the name matches any of the filenames (in an array of values named $overlayTargetFilenames containing Name.pdf Name2.pdf Name3.pdf) but with an exact match:
$matchedDrawings = Get-ChildItem $basePath -Directory -Force -Exclude 'Undesired 1','Undesired 2' | Get-ChildItem -Directory -Force | Get-ChildItem -Force -Recurse -Include '*.pdf' | where {$_.FullName -like '* A Target Subfolder\*' -and $_.FullName -notmatch "\\OLD" -and $_.Name -iin $overlayTargetFilenames} | sort($_.basename) | ForEach-Object {$_.FullName}
What I'm trying to do is figure out how to do a similar search for PDFs where the array of values has wildcards, such as how to make -iin (which is case insensitive "IN" operator, reverse argument order of -icontains) work like -ilike does for wildcards.
How must I modify the code above to work if the $overlayTargetFilenames had values like Name_* Name-2_* More-Name3_* -- looking for any basenames starting with those different searches? The sought names are generated earlier by the code, and the list size varies wildly.
I've tried -iin and -ilike.
You can do a regex match on values piped into a Foreach-Object
. For values that may have special regex characters, you can use the Escape()
method from the .NET Regex class. The result will be a string with all the escape characters needed for regex.
$Collection | Foreach-Object {
$regex = [Regex]::Escape($_)
}