Powershell Find String in file - How to use windows find command

powershell find string in file

If you are here, Then you might have had a similar requirement I had,  which is to find a specific file across multiple directories and to grep or Search for a string present in that file. ( more like what we do with xargs in Linux)

I am a Linux user and If you would have searched for "Windows Find command with Xargs" to come here then you are too.

Being used to the Linux commands like Find, Grep, Xargs  I was really finding it hard when I had a requirement like given below.

I was looking for a file named "App.properties" and wanted to look for a specific property or String and see if it is enabled or not.  "pushnotification.enabled=true"

I would have simply used something like Git Bash or someother Bash emulators for windows but I wanted to give it a chance to PowerShell and I was really impressed cause it is really powerful than I thought it was.

Enough of Bla Bla Bla. Let us get into the technical stuff.

This article is about how to use Windows PowerShell find command to find string in file.

Hope you understood my requirement.

Now, We can decouple the requirement into two different sub tasks

  1.  Find the file or Files in a Specific directory
  2. Look for a String in that matching/resulted files

 

In Linux we would have been able to do the same with the following command

find /somedir -type f -name "App.properties" |xargs grep -i  "pushnotification.enabled=true"

 

In Windows, The same can be acheived with the following powershell command

Get-ChildItem -Path D:\SomeDir -File -Filter  "App.properties"|Select-String "pushnotification.enabled=true"

 

To know, How this command has been created and know how it is working. Please continue to read.

 

Task 1: Find the File or Files in a Specific Directory

Now our first objective is to find the files matching our criteria. Here in our case we need to find the files with the name "App.properties"

Before going straight into our requirement, Let me give some background and brief on how we are going to do it.

Windows Equivalent to Find or ls Command

If you are thinking of Dir command of  Windows Command Prompt. I should shake your hand cause thats exactly what I thought before being introduced to the Powershell commands

Windows Powershell is giving you a powerful alternative for the Linux find and ls

Get-ChildItem

[[-Path] <string[]>]
[[-Filter] <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Recurse]
[-Depth <uint32>]
[-Force]
[-Name]
[-Attributes <FlagsExpression[FileAttributes]>]
[-FollowSymlink]
[-Directory]
[-File]
[-Hidden]
[-ReadOnly]
[-System]
[<CommonParameters>]

Yes this childish Get-ChildItem is the game changer and provides a lot of features.  If you read through the Syntax of the command itself you could understand what it offers.

Some of Linux Commands and their Windows Equivalents with Get-ChildItem are listed below.

Note*: A Period/Dot (.) in the command represents the present working directory

Purpose

Linux Command

 Windows Equivalent/alternative

Simple List with Reverse Sort by Date In Present Working Directory
ls -lrt .
 Get-ChildItem .
List only Directories
ls -lrtd /SomeDir/*
 Get-ChildItem -Path D:\somedir\ -Directory
Look for a File with Find command
find GithubRepo -type f -name "*.properties"
Get-ChildItem -Path D:\GithubRepo\ -Filter "*.properties" -Recurse -File
Recursive Listing including All Sub Directories
ls -lrRt
Get-ChildItem -Recurse
Print only the names of Files and Directories
ls -lrt GithubRepo|awk '{print $9}' (or) ls -a|sort
Get-ChildItem -Path D:\GithubRepo  -Name

 

There are more I can continue to list but I already feel like I am sidetracked.

Now I hope you might have got a good grip on the Get-ChildItem command and have already figured out the first part of the puzzle or completed the first objective.

If not, Here is the command

Get-ChildItem -Path D:\SomeDir -File -Filter  "App.properties"

 

 

 

Task 2: Look for a String in the matching/resulted files

Now we got the files now we need to traverse through the resulted files and search for the string that we are searching for, Thats our Second Objective or Second Part of the puzzle.

Windows Equivalent to xargs -grep Command

PowerShell gives us an another command named Select-String to replace the linux xargs -grep part.

Now take the final outcome of the previous objective and append this Select-String  to make the command complete

Get-ChildItem -Path D:\SomeDir -File -Filter  "App.properties"|Select-String "pushnotification.enabled=true"

 

 

Result: Final Command and its Output

This is the final command you were looking for.

Get-ChildItem -Path D:\SomeDir -File -Filter  "App.properties"|Select-String "pushnotification.enabled=true"

 

I went and executed the command and this was the result looked like.

For Better understanding, I have created a video with my Linux and PowerShell terminals Side by side and compared the Linux and Windows methods of getting my requirement done.

 

 

Hope this article helps.

Rate this article [ratings]

Cheers
Sarav AK

Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel

Buy Me a Coffee at ko-fi.com

Signup for Exclusive "Subscriber-only" Content

Loading