To do a case-sensitive find and replace across multiple files, turn on the match case option in whatever tool you use: the Match Case button (Alt+C) in VS Code's search, the Match case box in Notepad++ Find in Files, or -creplace instead of -replace in PowerShell. With it on, replacing Apple leaves apple and APPLE alone. To change every capitalization while keeping each one's style, add a separate case-sensitive rule for each form, or use VS Code's Preserve Case option.
The details matter because several common tools ignore case by default. Here is how each one behaves, how to keep capitalization intact, and the cases where case-sensitivity is essential.
Which tools ignore case by default?
| Tool | Default | Case-sensitive option |
|---|---|---|
PowerShell -replace | Ignores case | Use -creplace |
PowerShell .Replace() method | Case-sensitive | Already exact (literal text, no regex) |
PowerShell Select-String | Ignores case | Add -CaseSensitive |
| VS Code search | Ignores case | Match Case button or Alt+C |
| Notepad++ Find in Files | Ignores case | Tick Match case |
Windows findstr | Case-sensitive | Add /i to ignore case |
.NET [regex]::Replace | Case-sensitive | Pass IgnoreCase to ignore case |
The PowerShell row catches people most often. A script that runs -replace 'Apple','Orange' also turns apple pie into Orange pie, with a capital O, because the replacement text is inserted exactly as written.
PowerShell: -creplace
To replace a word case-sensitively in every .txt file in a folder tree:
Get-ChildItem -Recurse -File -Filter *.txt | ForEach-Object { $t = Get-Content $_.FullName -Raw; $n = $t -creplace '\bApple\b', 'Orange'; if ($n -cne $t) { Set-Content $_.FullName $n -NoNewline } }
Note the -cne in the comparison too. The ordinary -ne ignores case, so if the only change were a capitalization fix, such as javascript to JavaScript, -ne would decide nothing had changed and skip writing the file. Every PowerShell comparison operator has a c version for this reason: -ceq, -cne, -clike, -cmatch.
If you do not need regex, the string method is simpler and always case-sensitive: $t.Replace('Apple','Orange'). More PowerShell detail is in replacing text in multiple files with PowerShell.
Keeping capitalization when you replace
A rename usually needs to handle every form of a word while keeping each one's style: widget to gadget, Widget to Gadget, WIDGET to GADGET. There are three ways to do it.
1. One case-sensitive rule per form
The most reliable way, and the easiest to review:
Widget→Gadget(match case on)widget→gadget(match case on)WIDGET→GADGET(match case on)
Because each rule is case-sensitive, they never interfere with each other. Run a case-insensitive search afterwards to find any odd form you missed, such as WidGet in an old file.
2. VS Code's Preserve Case
In VS Code's replace box, the AB button (Alt+P) turns on Preserve Case. With match case off and preserve case on, replacing widget with gadget writes gadget, Gadget or GADGET to match each original. It handles all-lowercase, all-uppercase and first-letter-capital forms. Check mixed forms such as WidgetPro by eye.
3. Regex groups for the first letter
When the old and new words start with the same letter, a group can carry the capitalization: find \b([Pp])rogram\b, replace $1roject. Program becomes Project and program becomes project. When the first letters differ, use option 1.
Case-sensitive for only part of a pattern
.NET and PCRE-style engines, including PowerShell and Notepad++, support inline modifiers that switch case-sensitivity for one part of a pattern:
(?i:error)matches error, Error or ERROR, even when the rest of the pattern is case-sensitive.(?-i:ID)requires exactly ID, even when the rest ignores case.(?i)at the very start makes the whole pattern ignore case.
Example: -creplace '(?i:status):\s*OK', 'Status: Done' accepts any capitalization of the label but still requires exactly OK. The regex cheat sheet lists the other inline flags.
When case-sensitivity is essential
| Situation | Example | Risk without match case |
|---|---|---|
| Names vs ordinary words | Apple the company, apple the fruit | Both change |
| Abbreviations | US vs us, IT vs it | Every "us" and "it" in the text changes |
| Code identifiers | userId vs UserId | A property and a class get the same new name |
| Constants | MAX_SIZE vs max_size | A constant and a variable merge |
| Case-sensitive file systems and URLs | /Images/Logo.png on a Linux web server | Links break when case changes |
The abbreviation case is worth stressing. Replacing US with United States without match case and whole word would wreck nearly every paragraph. Use both: \bUS\b with match case on. Our guide to whole-word find and replace covers the other half of that combination.
A note on Windows file names: Windows treats Report.txt and report.txt as the same file, but a web server running Linux does not. If you change the case of names in links, make sure the files themselves are renamed to match before you upload.
First, see which capitalizations exist
Before writing case-sensitive rules, find out which forms the files actually contain. This PowerShell command searches without regard to case, then groups the results by exact spelling:
Get-ChildItem -Recurse -File -Include *.md,*.html | Select-String -Pattern '\bwidget\b' -AllMatches | ForEach-Object { $_.Matches.Value } | Group-Object -CaseSensitive | Select-Object Count, Name
The output might show 412 Widget, 37 widget and 5 WIDGET. That tells you exactly which rules you need, and the counts give you numbers to check the replace against afterwards.
Across many files with mixed rules
Real jobs mix both kinds of rule: a case-sensitive rule for a brand name and a case-insensitive one for a common misspelling. Bulk Replace sets case-sensitive and whole-word options per rule, so one run can include both, applied in order across the whole folder tree. The before/after diff shows exactly which capitalization each match had, which is the quickest way to confirm that apple pie was left alone. Every run is backed up first, so if a case-insensitive rule caught more than it should, Bulk Replace can undo the run in one click.
Quick reference
- PowerShell:
-creplace,-cne,-cmatch, or.Replace() - VS Code: Alt+C for match case, Alt+P for preserve case
- Notepad++: tick Match case
- Inline:
(?i:...)to ignore case in part of a pattern,(?-i:...)to require it
Frequently asked questions
Is PowerShell -replace case-sensitive?
No. -replace ignores case by default. Use -creplace for a case-sensitive regex replace, or the .Replace() string method for literal case-sensitive text.
How do I keep the original capitalization when replacing?
Run a separate case-sensitive rule for each form, such as Widget to Gadget and widget to gadget. VS Code also has a Preserve Case option in its replace box.
How do I make one part of a regex case-insensitive?
Use an inline group such as (?i:word) in .NET and PCRE engines, which ignores case only inside that group.
