$Path = \"\\\\Server\\Share\"\n$SW = \"Office, Java\"\nGet-ChildItem -path $Path -Include *.txt -recurse | Select-String -Pattern $SW | OGV\n<\/code><\/pre>\ncan anyone explain why or what needs to be changed.<\/p>","upvoteCount":6,"answerCount":13,"datePublished":"2018-06-26T11:53:33.000Z","author":{"@type":"Person","name":"robertrawers3","url":"https://community.spiceworks.com/u/robertrawers3"},"acceptedAnswer":{"@type":"Answer","text":"
Try this? Your original worked for me, and this did as well.<\/p>\n
$Path = \"\\\\Server\\Share\"\n[string]$SW = \"Office, Java\"\nGet-ChildItem -path $Path -Include *.txt -recurse | Select-String -Pattern $SW | OGV\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-06-26T16:12:51.000Z","url":"https://community.spiceworks.com/t/powershell-script-not-working/658854/11","author":{"@type":"Person","name":"ethanlekki","url":"https://community.spiceworks.com/u/ethanlekki"}},"suggestedAnswer":[{"@type":"Answer","text":"Hello everyone, Below is a sample of a code for parsing through some text files for some words.<\/p>\n
It works with:<\/p>\n
$Path = \"\\\\Server\\Share\"\nGet-ChildItem -path $Path -Include *.txt -recurse | Select-String -Pattern \"Office, Java\" | OGV\n<\/code><\/pre>\nbut doesn’t work with:<\/p>\n
$Path = \"\\\\Server\\Share\"\n$SW = \"Office, Java\"\nGet-ChildItem -path $Path -Include *.txt -recurse | Select-String -Pattern $SW | OGV\n<\/code><\/pre>\ncan anyone explain why or what needs to be changed.<\/p>","upvoteCount":6,"datePublished":"2018-06-26T11:53:33.000Z","url":"https://community.spiceworks.com/t/powershell-script-not-working/658854/1","author":{"@type":"Person","name":"robertrawers3","url":"https://community.spiceworks.com/u/robertrawers3"}},{"@type":"Answer","text":"
what error are you getting?<\/p>","upvoteCount":0,"datePublished":"2018-06-26T12:01:38.000Z","url":"https://community.spiceworks.com/t/powershell-script-not-working/658854/2","author":{"@type":"Person","name":"brad","url":"https://community.spiceworks.com/u/brad"}},{"@type":"Answer","text":"
Just a guess, but you might try<\/p>\n
$SW = \"*Office*\"\n\n# or\n\n...Select-String -Pattern \"*$SW*\"...\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-06-26T12:03:15.000Z","url":"https://community.spiceworks.com/t/powershell-script-not-working/658854/3","author":{"@type":"Person","name":"robertriceii","url":"https://community.spiceworks.com/u/robertriceii"}},{"@type":"Answer","text":"hmm I justed tested and worked as is on my systems<\/p>","upvoteCount":0,"datePublished":"2018-06-26T12:06:38.000Z","url":"https://community.spiceworks.com/t/powershell-script-not-working/658854/4","author":{"@type":"Person","name":"brad","url":"https://community.spiceworks.com/u/brad"}},{"@type":"Answer","text":"
I wasn’t able to reproduce your issue. Matching -pattern to a string variable versus a string gave me the same results.<\/p>\n
Maybe try to add -SimpleMatch and see what happens? That will tell PowerShell to interpret $SW as a string and not attempt RegEx.<\/p>\n
Get-ChildItem -path $Path -Include *.txt -recurse | Select-String -Pattern $SW -SimpleMatch | OGV\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-06-26T12:09:41.000Z","url":"https://community.spiceworks.com/t/powershell-script-not-working/658854/5","author":{"@type":"Person","name":"tahin","url":"https://community.spiceworks.com/u/tahin"}},{"@type":"Answer","text":"\n\n
<\/div>\n
RobertR_TX:<\/div>\n
\nJust a guess, but you might try<\/p>\n
$SW = \"*Office*\"\n\n# or\n\n...Select-String -Pattern \"*$SW*\"...\n\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nNO. The pattern is a regex not a wild card.<\/p>","upvoteCount":1,"datePublished":"2018-06-26T12:13:30.000Z","url":"https://community.spiceworks.com/t/powershell-script-not-working/658854/6","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Br@d:<\/div>\n
\nwhat error are you getting?<\/p>\n<\/blockquote>\n<\/aside>\n
the first way works as intended.<\/p>\n
the second outputs nothing<\/p>","upvoteCount":0,"datePublished":"2018-06-26T12:50:20.000Z","url":"https://community.spiceworks.com/t/powershell-script-not-working/658854/7","author":{"@type":"Person","name":"robertrawers3","url":"https://community.spiceworks.com/u/robertrawers3"}},{"@type":"Answer","text":"
i updated the scripts above.<\/p>\n
it has an issue when you have multiple patterns<\/p>","upvoteCount":0,"datePublished":"2018-06-26T12:53:17.000Z","url":"https://community.spiceworks.com/t/powershell-script-not-working/658854/8","author":{"@type":"Person","name":"robertrawers3","url":"https://community.spiceworks.com/u/robertrawers3"}},{"@type":"Answer","text":"
So I did this as work around:<\/p>\n
$PATH = \"\\\\server\\share\"\n$SW = \"Office\",\"Java\"\nForeach ($S in $SW) {\nget-childitem -path $Path -Include *.txt -Recurse | Select-String -Pattern $S\n}\n<\/code><\/pre>\nstill curious why it doesn’t work the other way<\/p>","upvoteCount":0,"datePublished":"2018-06-26T12:56:40.000Z","url":"https://community.spiceworks.com/t/powershell-script-not-working/658854/9","author":{"@type":"Person","name":"robertrawers3","url":"https://community.spiceworks.com/u/robertrawers3"}},{"@type":"Answer","text":"
This works for me:<\/p>\n
$Path = \"\\\\Server\\Share\"\n$SW = \"Office\", \"Java\"\nGet-ChildItem -path $Path -Include *.txt -recurse | Select-String -Pattern $SW | OGV\n<\/code><\/pre>\nThis matched “Office” OR “Java”.<\/p>","upvoteCount":1,"datePublished":"2018-06-26T13:09:53.000Z","url":"https://community.spiceworks.com/t/powershell-script-not-working/658854/10","author":{"@type":"Person","name":"tahin","url":"https://community.spiceworks.com/u/tahin"}},{"@type":"Answer","text":"
use single quotes for $SW<\/p>","upvoteCount":0,"datePublished":"2018-06-27T08:21:29.000Z","url":"https://community.spiceworks.com/t/powershell-script-not-working/658854/12","author":{"@type":"Person","name":"britv8","url":"https://community.spiceworks.com/u/britv8"}},{"@type":"Answer","text":"
-Pattern is using RegEx so we should be using the Regular Expression syntax, so:<\/p>\n
$PATH = \"\\\\server\\share\"\n$SW = \"Office|Java\"\nget-childitem -path $Path -Include *.txt -Recurse | Select-String -Pattern $SW\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-06-27T09:13:36.000Z","url":"https://community.spiceworks.com/t/powershell-script-not-working/658854/13","author":{"@type":"Person","name":"martin9700","url":"https://community.spiceworks.com/u/martin9700"}}]}}
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
brad
(Br@d)
June 26, 2018, 12:01pm
2
what error are you getting?
Just a guess, but you might try
$SW = "*Office*"
# or
...Select-String -Pattern "*$SW*"...
brad
(Br@d)
June 26, 2018, 12:06pm
4
hmm I justed tested and worked as is on my systems
tahin
(TAHIN)
June 26, 2018, 12:09pm
5
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
DoctorDNS
(DoctorDNS)
June 26, 2018, 12:13pm
6
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
tahin
(TAHIN)
June 26, 2018, 1:09pm
10
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
britv8
(britv8)
June 27, 2018, 8:21am
12
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