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
0
votes
1 answer

Find latest matching entry from log file

I have a log file, which is being written to constantly throughout the day. I need a way to check the logfile, and find the last entry that matches a specific string. This entry could be 5 rows from the end or 5000 rows. I'm reluctant to read the…
IGGt
  • 2,627
  • 10
  • 42
  • 63
0
votes
1 answer

Powershell LastWriteTime each item that is matched using select-string -pattern

Attempting to look though a list of files and parse out the phone numbers using regex in powershell, but I also want to include the LastWriteTime of each file I am currently using this but it seems to only pull the Date info I need from the first…
O.C.
  • 81
  • 9
0
votes
1 answer

How to make select-string only match records that are 6 characters long?

I’m creating a script that reads a text file and compares the results to an array. It works fine, but I have some records that say they match but they don’t. For example - TG1032 and TG match according to the select-string script. Here is my…
0
votes
1 answer

Powershell Failed to get member output

I am trying use Regex to out the KB number out of ProductName. All I get is an empty string. gwmi -cl win32_reliabilityRecords -filter "sourcename = 'Microsoft-Windows-WindowsUpdateClient'" | select @{LABEL = 'KB#' ; EXPRESSION = {(Select-string…
Aaron
  • 563
  • 3
  • 13
0
votes
1 answer

Powershell: How to do a Select-String search with an -Exclude for multiple lines of text in another file

Project: I have a log file that is being added to continuously. In this log file on occasion an error (error always includes with "Unable to import" and a unique ID) is added. I need to be alerted by email when this error is added to the log…
Kram_Koorbse
  • 442
  • 5
  • 19
0
votes
1 answer

Powershell select-string not working as planned for an pattern array

I am trying to search a log file for certain times which are stored in an array. I am then passing the matches back to a variable for further processing, but I am stumbling at the first hurdle. I am trying to pattern match with the…
eboni
  • 883
  • 2
  • 10
  • 25
0
votes
1 answer

Replace different occurences of String with different values in powershell?

I am pretty new to powershell scripting.The scenario is that I have to replace the first occurrence of a string with different value and second occurrence with a different value. So far, I have this : $dbS = Select-String …
Shweta
  • 924
  • 14
  • 23
0
votes
1 answer

Powershell select-string Matching x AND y

I'm trying to do a search of some log files for lines that contain certain strings. The files contain multiple lines like: ALARM 11/08/2014 10:00:02,InFILE typeID,actionID,customerID: various_other_data_here ALARM 11/08/2014 10:00:03,OutFILE…
IGGt
  • 2,627
  • 10
  • 42
  • 63
0
votes
1 answer

find and replace only the first match in PowerSell

I want to replace: "created_at" with "`r`ncreated_at" but only the first time that occurrence in each line. This is my code: $regex="`"created_at`"" Get-Content twittermsgs.txt |%{$_-replace "`"metadata`"","`r`n"}|$regex.Replace($s,…
user3562182
  • 365
  • 1
  • 5
  • 8
0
votes
1 answer

Powershell move-item file in use

Trying to match a string in a bunch of CSV files and then move those with the string to another folder. The script logic seems to work but I keep getting errors that the file is use. I imagine it is powershell that has the file locked. How can I…
0
votes
3 answers

Parse text with powershell and display only what you were searching for

I'm trying to parse a text file with this format: \\fileshare40\abccheck\logons\ABC64ZXZ.txt:5398:UserID: abcusernamehere Logged: 09:18:36 2014/03/13 \\fileshare40\abccheck\logons\ABC63BZB.txt:5403:UserID: abcusernamehere Logged: 01:21:31…
0
votes
1 answer

Printing the whole line Powershell on word finder

$check = $args[1] $numArgs = $($args.count) $totMatch = 0 #reset variables for counting for ( $i = 2; $i -lt $numArgs; $i++ ) { $file = $args[$i] if ( Test-Path $file ) { #echo "The input file was named $file" $match =…
user2569803
  • 637
  • 6
  • 11
  • 19
0
votes
1 answer

Select-string to grab time format from a log file entry

I am trying to pull date and time from the similar string below:
1BilliumDollars
  • 149
  • 1
  • 3
  • 12
0
votes
2 answers

In Powershell can i use the select-string -quiet switch within a foreach statement

i am trying to write a script to check if a string is within a file, if it is then carry on and check the next one and continue until all are checked, if it finds one that is not present then the script will terminate. Am i able to use a…
Mach1
  • 13
  • 1
  • 1
  • 5
0
votes
2 answers

Powershell how to take string from file and put into a variable

I am writing a Powershell script to "build" Windows 7 PCs: adding users, printers, applications, changing settings, et al. I am adding some printer drivers using PNPUtil, but the problem is I won't know what "Published name" the drivers will be…
BigRedEO
  • 807
  • 4
  • 13
  • 33
1 2 3
15
16