Hello everyone, Below is a sample of a code for parsing through some text files for some words.

It works with:

$Path = "\\Server\Share"
Get-ChildItem -path $Path -Include *.txt -recurse | Select-String -Pattern "Office, Java" | OGV

but doesn’t work with:

$Path = "\\Server\Share"
$SW = "Office, Java"
Get-ChildItem -path $Path -Include *.txt -recurse | Select-String -Pattern $SW | OGV

can anyone explain why or what needs to be changed.

6 Spice ups

what error are you getting?

Just a guess, but you might try

$SW = "*Office*"

# or

...Select-String -Pattern "*$SW*"...

hmm I justed tested and worked as is on my systems

I wasn’t able to reproduce your issue. Matching -pattern to a string variable versus a string gave me the same results.

Maybe try to add -SimpleMatch and see what happens? That will tell PowerShell to interpret $SW as a string and not attempt RegEx.

Get-ChildItem -path $Path -Include *.txt -recurse | Select-String -Pattern $SW -SimpleMatch | OGV

NO. The pattern is a regex not a wild card.

1 Spice up

the first way works as intended.

the second outputs nothing

i updated the scripts above.

it has an issue when you have multiple patterns

So I did this as work around:

$PATH = "\\server\share"
$SW = "Office","Java"
Foreach ($S in $SW) {
get-childitem -path $Path -Include *.txt -Recurse | Select-String -Pattern $S
}

still curious why it doesn’t work the other way

This works for me:

$Path = "\\Server\Share"
$SW = "Office", "Java"
Get-ChildItem -path $Path -Include *.txt -recurse | Select-String -Pattern $SW | OGV

This matched “Office” OR “Java”.

1 Spice up

Try this? Your original worked for me, and this did as well.

$Path = "\\Server\Share"
[string]$SW = "Office, Java"
Get-ChildItem -path $Path -Include *.txt -recurse | Select-String -Pattern $SW | OGV

use single quotes for $SW

-Pattern is using RegEx so we should be using the Regular Expression syntax, so:

$PATH = "\\server\share"
$SW = "Office|Java"
get-childitem -path $Path -Include *.txt -Recurse | Select-String -Pattern $SW