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
4
votes
2 answers

Powershell select-string returns different values depending on number of matches

I have a report file that is generated and containes various file references. I am using Select-String and regular expressions to match on certain types of files and perform subsequent processing on them. the dilemma I have is trying to consistently…
3
votes
2 answers

How to define a command alias in PowerShell for filtering output?

I need to define an alias for this: Select-String -NotMatch -Pattern "^[\t ]+\d" so that I can use the alias instead of writing that long string each time. After googling for 5 minutes and doing some experiments I came up with this: filter foo { …
Mercalli
  • 700
  • 6
  • 16
3
votes
1 answer

Coloring array filters in PowerShell

Trying to figure out how to color certain filters within an array for ease of reading on console. In other words, as the console progress forward, if "[Behaviour] OnPlayerJoined" appears, color that text green. $filters = @( ##################…
Atomiklan
  • 5,164
  • 11
  • 40
  • 62
3
votes
2 answers

select-string use in a for-each loop

I am working on building a script that will access a large amount of files and pull a specific string out in order to assign as a variable. The string is similar across all files which is not the issue. I am able to make this process function as an…
Steve Martin
  • 75
  • 1
  • 6
3
votes
1 answer

PowerShell Select-String context doesn't work with output piped from non-PowerShell command

I'm trying to use Select-String in PowerShell to look at the results from docker inspect command, but the Context property doesn't seem to be working properly. As you know, docker inspect returns normal JSON as output, e.g.: [ { "Id":…
Farzad
  • 1,770
  • 4
  • 26
  • 48
3
votes
3 answers

Powershell search strings in a file according to arguments

I have a file with multiple words. I would like to get only those words, that contain the letters I passed as arguments to the program. For example: test.txt apple car computer tree ./select.ps1 test.txt o e r Result should be like: computer I…
Joe
  • 33
  • 5
2
votes
1 answer

Searching for string in text file, but getting multiple results

I'm trying to search for 172.21.134.16 specifically, but I get unwanted rows of others found (ie 172.21.134.161, 162, 163, etc. I just like the results to be 172.21.134.16. PS C:\Scripts\Ping> Get-ChildItem -path c:\scripts\ping\hostnames.txt…
Virtute
  • 21
  • 1
2
votes
2 answers

Select-string patern match and notmatch

I am fairly new to Powershell and have run into a bit of a pickle. I'm trying to find string "o_dwh" in scripts, but if there is exec before this statement like this - "exec o_dwh" - I don't want to select that. How do I do that? So far I have…
2
votes
1 answer

Powershell - match sid on variable not working

I have a relatively straight forward script. Export secedit Look for a specific SIDs and store them For each of the SIDs, return the group name However, when I try to filter the results of Get-LocalGroup based on the SID value, I get no results.…
Kuba
  • 165
  • 7
2
votes
1 answer

Powershell/Regex to count number of times a date range appears

I am working on a script to pull metrics from a log file. Here is an example from the log. 2/21/2022 3:29 PM: Requested username - Found disabled account with matching CATIID named username - Account username reactivated 3/21/2022 3:37 PM: username…
Rich
  • 47
  • 5
2
votes
2 answers

Powershell Select-string text file

I am fairly new to Powershell and have run into a bit of a pickle. I have created a list of all files in multiple directories with their extensions with the aim of doing a string pattern match to retrieve the files (with their extension and matching…
cwon
  • 21
  • 3
2
votes
1 answer

Using select-string to output list from 1000+ files

I'm trying to get a specific string from a text file and output it and the filename to a separate txt file. I have tried the following but get an error message. I've looked for answers but haven't found any. Any help is appreciated. I Should add…
tinman
  • 21
  • 1
2
votes
1 answer

How do I match on wsl output in powershell?

I want to see if I'm running a particular wsl distribution (Windows 10 Home, WSL 2): PS C:\Users\User> wsl --list --running Windows Subsystem for Linux Distributions: Ubuntu (Default) MyDistro PS C:\Users\User> wsl --list --running | Select-String…
Eric
  • 2,115
  • 2
  • 20
  • 29
2
votes
1 answer

Powershell select-string not finding ÿ

I've got a log file with some bad characters in it. While there are many, the one I'm specifically interested in at the moment is ÿ When I try to do a simple select-string with it, I get no results at all: select-string -path D:\logs\*.log -Pattern…
TulsaNewbie
  • 382
  • 3
  • 15
2
votes
1 answer

How to Select-String from Multiple Lines with Powershell

I have this file below test.dat Games Applications Demos Games MLB 2002…
volitank
  • 27
  • 1
  • 5
1
2
3
15 16