Powershell Find All Files With String



  1. Powershell Search For Text In Files
  2. How To Find A File With Powershell
  3. Powershell Find Files With String In Name

Date Published: 10 October 2009

This week I found myself wanting to search within files of a given extension for a particular substring. I often find myself missing UNIX’s grep tool. In any event, I tried using the default Windows Vista file search dialog, but found that if I wanted to search for “connection” or “database” within all files ending with “.cs” or “.config” I was unable to do so. I’m guessing there actually *is* a way to do this from the GUI, but after spending a couple of minutes either searching all files for the text “.cs” or else searching for files named “connection” I opted to just do it from the command line using PowerShell.

Powershell Find All Files With String

Powershell Search For Text In Files

In Powershell list all files in a directory is a very easy task. We need to only use the following command to just list all the files only in a directory. Table of contents: PowerShell List files in a Directory. Gci alias for get-chiltitem. Dir alias for get-childitem. PowerShell list only directories.

PowerShell is just freaking amazing. Seriously. Being able to easily store collections of objects in ad-hoc variables (named $something) and then to further be able to pipe the output of anything to anything (using | just like most other shells), you can quickly do some amazing stuff. I use PowerShell all the time now to make downloading pictures from my camera and organizing them by date easy

To get a list of all files with a given extension that contain a given string, you can use the following steps (click on the screenshot to see it full size)

Button
  1. Move to the appropriate folder. While not strictly necessary, this will make your life easier. The standard “cd” command from DOS works for this, but remember you can’t do “cd” you have to do “cd ” to move to the root of the current volume.
  2. Get a list of all files. This isn’t strictly required but again using an intermediate step like this can ensure you catch any mistakes in your operation earlier. To get a collection of all the files in and below your current folder, use:$AllFiles = Get-ChildItem –recurse
  3. Next we’ll filter our list to only include files with the extension we want. If we’re looking for all of our C# source files, we would search for extensions that end with “.cs”. Note that the $_ value refers to the current item in the collection as we loop through them applying our filter condition. The “-eq” means “equals”. The full command to create a new collection named $CSharpFiles is:$CSharpFiles = $AllFiles | where {$_.extension –eq “.cs” }
  4. The next step is again not strictly necessary since we could simply write out the command, but it does show another useful PowerShell feature, namely the ability to alias commands with shorthand strings. For example, the Select-String command is useful for matching files that contain a particular string, but if you’d prefer to just type “ss” for this, you can alias it like so:Set-Alias ss Select-String
  5. Finally, we’ll use the ss alias on our filtered list of files to find all files containing the string “SqlConnection” which might be useful if you’re trying to ensure nobody is using ADO.NET when they should be using your ORM:ss SqlConnection $CSharpFiles | format-table Path
All

The result will be the paths of all files that contain that string. If you leave off Path from the end of the command, your table will include the line number and the actual Line of the file that matched the string you specified.

So there you go. It took much longer to write this blog post than to figure all of this out in PowerShell and find the files I was after. Here are a couple of links that helped me get here:

Select-String uses the Path parameter with the asterisk (.) wildcard to search all files in the current directory with the file name extension.txt. The Pattern parameter specifies the text to match Get. Select-String displays the output in the PowerShell console. Searching content in files. A first method to search text string in files (not in filenames) we can use the Select-String cmdlet that expects the path as well as the pattern parameter to be strings, so we do not need to use quotation marks for either the pattern or the path. Search for all.txt files Get the contents of those files Match the contents of those.txt files each entry in a control file (controlfile.hican) If there is a match on one of the strings in the control file and the.txt file, the match will be written to an output log file.

Category - Browse all categories

Software Architect

Steve is an experienced software architect and trainer, focusing on code quality and Domain-Driven Design with .NET.

  • 16-Feb-2014
  • 5

How to use PowerShell to (grep) Recursively Search for Text Within Files on Windows

Category : How-to

The thing I find most annoying with Windows is that it isn’t Linux. Let’s forget the argument of free software, the interchangeable GUIs, the security and everything else which constitutes the usual Linux vs. Windows argument and focus on things I use everyday in Linux which are missing in Windows. Two major things come to mind; tail for monitoring logs and grep which is the easiest way to find something in a file.

Not having grep, more specifically grep -r, is challenging at best and almost reason enough to avoid the platform entirely.

With the introduction of PowerShell, Windows has given us the grep functionality albeit with a much less finesse than the Linux equivalent. You have to pipe multiple commands together; one command to transverse the directories, and one command to look for the pattern within each file found.

Use the below command inside the directory you would like to perform the ‘grep’ and change [SEARCH_PATTERN] to match what you would like to match.

For example:

How To Find A File With Powershell

As you can see, its nowhere near the memorable Linux command grep -r but at least its now possible go get similar behaviour in a Windows environment.

Other Windows grep Binaries

Powershell Find Files With String In Name

There are also various Windows binaries which can be used from a standard command prompt however I had limited luck with each one. The biggest issue was that they require dependencies such as .NET which are not usually installed in server environments.