Questions tagged [select-string]

Select-String is a powershell cmdlet that finds text in strings and files.

Select-String is a powershell cmdlet that finds text in strings and files.

The Select-String cmdlet searches for text and text patterns in input strings and files. You can use it like Grep in UNIX and Findstr in Windows. You can type "Select-String" or its alias, "sls".

Select-String is based on lines of text. By default, Select-String finds the first match in each line and, for each match, it displays the file name, line number, and all text in the line containing the match. However, you can direct it to detect multiple matches per line, display text before and after the match, or display only a Boolean value (true or false) that indicates whether a match is found.

Select-String uses regular expression matching, but it can also perform a simple match that searches the input for the text that you specify.

Select-String can display all of the text matches or stop after the first match in each input file. It can also display all text that does not match the specified pattern. You can also specify that Select-String should expect a particular character encoding, such as when you are searching files of Unicode text.

230 questions
2
votes
2 answers

Question about using PowerShell Select-String, exiftool(-k)

In a PowerShell script I'm trying to filter the output of the exiftool(-k).exe command below, using Select-String. I've tried numerous permutations, but none work, and I always see the unfiltered output. What do I need to do to filter the output…
zBernie
  • 97
  • 2
  • 10
2
votes
3 answers

Efficiently extract matching lines including path and line number?

The following snippet extracts only the matching lines, I also want the path and line number: Get-ChildItem $thePath\ -Include "*.txt" -Recurse | Get-Content | Select-String -Pattern 'THE_PATTEN' | Set-Content "output.txt" I tried with this method…
Sherzad
  • 405
  • 4
  • 14
2
votes
1 answer

Regex to capture in multiline works on regex101.com but not in PowerShell

This is the regex I am using code (?m)^.*(10(?:\.\d+){3}\/\d+)\s.*\s(\d+).*$ It is suppose to catch subnet IP and last 4 digits from the line. One exception being if there is only the subnet IP directly followed by new line, then the capture should…
2
votes
3 answers

Combine two regex in PowerShell

This is the data I am trying to parse: 10.186.128.0/20 172.17.128.161 0 65000 8788 10.186.128.0/20 172.17.128.161 0 65000 878 10.186.128.0/20 172.17.128.161 0 65000…
2
votes
1 answer

How to search content in files in Powershell

I want to make a dynamic function that searches for the requested $ErrorCode within the files inputted and eventually copy the files with the error to another folder. Right now, my code takes only one file and returns the sentence of where the…
jgt05
  • 27
  • 6
2
votes
2 answers

How to find and replace all occurrences of "(percentage)%" in a text file and replace with a user supplied Integer

I am trying to parse through a text file with regex finding percentages as strings and replacing the findings with the percentage multiplied by a user supplied integer. If the user inputs 400, then the code should return "120 x 8, 180 x 6,…
2
votes
3 answers

Using Select-String to Match Multiple Single-Line Patterns and Write to Output

I am trying to build a simple script to utilize regex and match multiple patterns on a single line - recursively throughout an input file, and write the result to an output file. But I'm hitting a wall: Sample text: BMC12345 COMBINED PHASE…
Jonmonjovi
  • 23
  • 1
  • 7
2
votes
2 answers

Ignore a character when using Select-String

I am looking for a string of specific characters in a text file; I use Select-String -Path clm-server-test.txt -Pattern "#(S****)" I want to get only that characters matching the pattern, but it returns also characters before and after that…
Medazou
  • 21
  • 4
2
votes
4 answers

Taking the last word of each line from Select-String

I'm stuck on a problem pulling text from a file. Here's what I ultimately want to do: Search through a specific file for instances of the phrase "NoRequest". ONLY take the value of NoRequest. i.e. if NoRequest = true, then it would only add "true"…
2
votes
2 answers

Select-String output gives whole object and does not respond to filter

I have done some programming on Unix/Linux, but lately PowerShell has been more useful in my work. However the lack of grep drives me mad. Can someone explain this phenomenon? I am trying to get the value of IOTA for today returned to a value that I…
2
votes
1 answer

Return True if two patterns are found in text with Powershell

This seems like a simple task but I can't seem to get it to work. I want to be able to read a text file and if two exact patterns are found return True. If it only finds one then it returns false. I tried this, but it is not working: Get-Content…
Jason Murray
  • 147
  • 2
  • 4
  • 13
2
votes
1 answer

Match keywords (patterns) from file against multiple files

Sorry for the obscure title, here is my problem and what I want to do. My files: \\mydir\keywords.txt \\mydir\textfile1.txt \\mydir\textfile2.txt \\mydir\textfile3.txt etc I have "keywords.txt" with a lot of keywords (strings) that I want to match…
TheSwede86
  • 85
  • 2
  • 8
2
votes
1 answer

Select-String -Pattern Output when piping into

I try to get PowerShell to spit out a Semantic Versioning Variable but get stuck in it displaying just the command I entered (doing that in the ISE) or either of 2 errors ('missing argument' or 'doesn't accept piped input'), which, if I try to…
2
votes
2 answers

Select-String -Context and find email address in data

I have a text file where variations of this data (the number after 'SVC' and the date before, along with the text body) will appear multiple times. I can capture the string of data, but once I do, I need to locate an email address inside that data.…
Nate
  • 802
  • 1
  • 8
  • 28
2
votes
2 answers

Find and replace a string containing both double quotes and brackets

Let's say I have a test file named testfile.txt containing the below line: one (two) "three" I want to use PowerShell to say that if the entire string exists, place a line directly underneath it with the value: four (five) "six" (Notice that it…
coursemyhorse
  • 23
  • 2
  • 6
1 2
3
15 16