Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk7 min

How to Search Files for a Batch File or Command-Line String

Use findstr or PowerShell to search recursively inside .bat and .cmd files, or locate batch files by filename. This guide covers literal phrases, regex, filenames-only results, encodings, permissions, and missed matches.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To search inside Windows batch files for a command or phrase, run this in Command Prompt:

findstr /s /i /n /l /c:"your string" "C:Scripts*.bat" "C:Scripts*.cmd"

In PowerShell, use:

Get-ChildItem "C:Scripts" -Recurse -File -Include *.bat,*.cmd |
    Select-String -SimpleMatch -Pattern "your string"

These commands search the contents of .bat and .cmd files. If you only need to locate batch files by filename, use where /r or Get-ChildItem instead.

As an Amazon Associate I earn from qualifying purchases.

First decide whether you need filenames or file contents

“Find a batch file” can mean two different things:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Find files by name: locate files ending in .bat or .cmd.
  • Search file contents: find scripts containing a command, variable, phrase, or command-line string.

The commands below keep those tasks separate. Windows batch scripts commonly use both extensions, so searching only for *.bat can miss valid command scripts.

Find batch files by filename

In Command Prompt:

where /r "C:Scripts" *.bat
where /r "C:Scripts" *.cmd

In PowerShell:

Get-ChildItem -Path "C:Scripts" -Recurse -File -Include *.bat,*.cmd

In Windows Explorer, enter *.bat OR *.cmd in the search box. Filename searches locate scripts; they do not inspect the text inside them.

Search batch-file contents with Command Prompt

Microsoft’s built-in findstr searches text in the files specified by your filename patterns.

findstr /s /i /n /l /c:"ipconfig /all" "C:Scripts*.bat" "C:Scripts*.cmd"

The switches mean:

  • /s searches the specified directory and its subdirectories.
  • /i ignores letter case.
  • /n displays line numbers.
  • /l treats the search term as literal text instead of a regular expression.
  • /c: keeps a phrase containing spaces together as one search string.

A match may look like this:

C:Scriptsnetwork.bat:14:ipconfig /all

That output gives the path, line number, and matching line.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Search the current directory and its subdirectories

If Command Prompt is already open in the directory you want to scan, you can shorten the command:

findstr /s /i /n /l /c:"ipconfig /all" *.bat *.cmd

List only files that contain a match

Add /m when you want an inventory of matching files rather than every matching line:

findstr /s /i /m /l /c:"ipconfig /all" "C:Scripts*.bat" "C:Scripts*.cmd"

Search every file type

To search text files beyond batch scripts, use a broader filename mask:

findstr /s /i /n /l /c:"ipconfig /all" "C:Scripts*.*"

This may inspect logs, configuration files, source code, and unsuitable binary files. Restricting the search to known text extensions is usually faster and produces more useful results.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Search for regular-expression patterns

Without /l, findstr uses its own limited regular-expression syntax. For example:

findstr /s /i /r /n "set .* PATH" "C:Scripts*.bat"

Microsoft documents constructs including ., *, ^, $, character classes, and word-boundary forms such as < and >. This is not the same as a modern PCRE2 regular-expression engine.

Characters such as ., *, [, ], ^, and $ can have special meaning in regex mode. For an exact string, use /l and /c::

findstr /s /i /l /c:"C:Program FilesTooltool.exe" "C:Scripts*.bat"

Search for multiple terms

For single-word terms, provide several search strings:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
findstr /s /i /l /n "net use robocopy schtasks" "C:Scripts*.bat"

For phrases, use separate /c: arguments:

findstr /s /i /l /n /c:"net use" /c:"robocopy C:" "C:Scripts*.bat"

For a larger list, put one search term per line in searches.txt:

net use
robocopy
schtasks

Then run:

findstr /s /i /l /n /g:searches.txt "C:Scripts*.bat" "C:Scripts*.cmd"

The /g: switch reads search strings from a file. Microsoft also documents /f: for reading the list of files to search.

Search with PowerShell

PowerShell separates file selection from text matching. Get-ChildItem -Recurse supplies the files, and Select-String searches their contents.

Get-ChildItem "C:Scripts" -Recurse -File -Include *.bat,*.cmd |
    Select-String -SimpleMatch -Pattern "robocopy"

-SimpleMatch makes the pattern literal. Without it, Select-String interprets the pattern as a regular expression.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Return only one result per matching file

Get-ChildItem "C:Scripts" -Recurse -File -Include *.bat,*.cmd |
    Select-String -SimpleMatch -Pattern "robocopy" -List |
    Select-Object -ExpandProperty Path

-List stops after the first match in each file, while the final command prints only the paths.

Show surrounding lines

Get-ChildItem "C:Scripts" -Recurse -File -Include *.bat,*.cmd |
    Select-String -SimpleMatch -Pattern "robocopy" -Context 3,3

-Context 3,3 displays three lines before and three lines after each match, which is useful for understanding variables, conditions, and arguments around a command.

Use regex, case sensitivity, and encoding options

Regex search:

Get-ChildItem "C:Scripts" -Recurse -File -Include *.bat,*.cmd |
    Select-String -Pattern 'sets+PATH'

PowerShell matching is normally case-insensitive. Add -CaseSensitive when capitalization matters:

Get-ChildItem "C:Scripts" -Recurse -File -Include *.bat,*.cmd |
    Select-String -SimpleMatch -CaseSensitive -Pattern "Error"

Legacy batch files may use a Windows code page, UTF-8, or UTF-16. If a visible string is missed, specify the encoding after identifying the file’s format:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Select-String -Path "C:Scriptsexample.bat" `
    -Pattern "text" `
    -SimpleMatch `
    -Encoding UTF8

In the documented current PowerShell view, a byte-order mark is used when present and UTF-8 is otherwise assumed; older files may require an explicit encoding. PowerShell 7.4 and later also support the Ansi encoding value.

Search a protected location

Starting with a directory you own is preferable. Searches across C:, C:Windows, another user’s profile, or protected network locations can produce access errors. If you are authorized to inspect the location, an elevated PowerShell window may be required.

Get-ChildItem "C:" -Recurse -File -Include *.bat,*.cmd -ErrorAction SilentlyContinue |
    Select-String -SimpleMatch -Pattern "powershell.exe" -ErrorAction SilentlyContinue

Suppressing errors makes output cleaner but can hide files that were not readable. A no-match result from this command does not prove that the string is absent from the entire drive.

Use ripgrep for large or cross-platform searches

ripgrep (rg) is an optional command-line search tool available for Windows, macOS, and Linux. It searches recursively by default and provides precise file globs and exclusions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
rg -n -i -F --glob "*.bat" --glob "*.cmd" "robocopy C:Source" "C:Scripts"
  • -n shows line numbers.
  • -i ignores case.
  • -F performs a fixed-string search instead of regex matching.
  • --glob restricts the files searched.
  • -l lists matching filenames only.
  • -g "!vendor/**" excludes a directory.

For example, list only matching batch-script paths:

rg -l -i -F --glob "*.bat" --glob "*.cmd" "robocopy" "C:Scripts"

By default, ripgrep skips hidden files, binary files, and files excluded by .gitignore, .ignore, or .rgignore. To broaden the search:

rg -uuu -a -n -i -F "text" "C:Scripts"

-uuu relaxes automatic filtering, and -a treats binary input as text. These options can increase noise and produce unreadable output. For a known encoding, such as UTF-16:

rg -n -F -E utf-16 "robocopy" "C:Scripts"

Performance depends on the storage device, number and size of files, pattern, and filtering. Consult the project repository for supported platforms and releases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Search from Linux, macOS, WSL, or Git Bash

GNU grep provides the conventional recursive command:

grep -RniF --include='*.bat' --include='*.cmd' -- 'YOUR COMMAND OR STRING' /path/to/folder

The options mean recursive search (-R), line numbers (-n), case-insensitive matching (-i), and fixed-string matching (-F). To list only matching files:

grep -RliF --include='*.bat' --include='*.cmd' -- 'robocopy' /path/to/folder

The -- separates the search pattern from options, which is useful when the pattern begins with a hyphen. See the GNU grep manual for recursive, include, exclude, and binary-file behavior.

Why a search may find nothing

  1. Wrong root directory: confirm the path and try one known file first.
  2. Wrong extension: include both *.bat and *.cmd, or temporarily inspect the directory by filename.
  3. Literal text treated as regex: use /l, -SimpleMatch, -F, or -f as appropriate.
  4. Case mismatch: add /i or -i; PowerShell is normally case-insensitive unless -CaseSensitive is used.
  5. Unexpected encoding: retry with the file’s actual UTF-8, UTF-16, ANSI, or legacy code-page setting.
  6. Line breaks: ordinary searches are line-oriented. A command split with a caret or across multiple lines may not match as one phrase; search for a distinctive fragment on each line or use a multiline-capable workflow.
  7. Excluded files: ripgrep may skip hidden, ignored, or binary files until filtering is relaxed.
  8. Unreadable files: access-denied errors mean the scan was incomplete.

What these tools cannot search reliably

findstr, Select-String, rg, and grep are text-search tools, not parsers for Word documents, PDFs, ZIP archives, databases, or compiled executables. Extract the text first when necessary. ripgrep can use options such as -z for supported compressed input or --pre to pass files through a preprocessor such as pdftotext, but those options do not automatically understand every file format.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick reference

Goal Command
Search batch contents in Command Prompt findstr /s /i /n /l /c:"text" "C:Folder*.bat" "C:Folder*.cmd"
List matching batch filenames findstr /s /i /m /l /c:"text" "C:Folder*.bat" "C:Folder*.cmd"
Search batch contents in PowerShell Get-ChildItem "C:Folder" -Recurse -File -Include *.bat,*.cmd | Select-String -SimpleMatch -Pattern "text"
List matching paths in PowerShell Get-ChildItem "C:Folder" -Recurse -File -Include *.bat,*.cmd | Select-String -SimpleMatch -Pattern "text" -List | Select-Object -ExpandProperty Path
Search with ripgrep rg -n -i -F --glob "*.bat" --glob "*.cmd" "text" "C:Folder"
Search with GNU grep grep -RniF --include='*.bat' --include='*.cmd' -- 'text' /path/to/folder
Find batch files by name where /r "C:Folder" *.bat and where /r "C:Folder" *.cmd

Safety reminder

Searching files is generally read-only. Opening an unfamiliar .bat or .cmd file in a text editor is safer than executing it, but do not run a script merely because your search found it. Batch files can delete files, change system settings, download programs, or launch other scripts.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.