# How to replace string case insensitive?

**URL:** <https://forums.powershell.org/t/how-to-replace-string-case-insensitive/13710>\
**Category:** PowerShell Help\
**Created:** [January 25, 2020, 2:35pm UTC](https://forums.powershell.org/t/how-to-replace-string-case-insensitive/13710 "2020-01-25T14:35:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stax76](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@stax76](https://forums.powershell.org/u/stax76)\
**Post date:** [January 25, 2020, 2:35pm UTC](https://forums.powershell.org/t/how-to-replace-string-case-insensitive/13710/1 "2020-01-25T14:35:34Z")

</div>

Hello,

how would I do case-insensitive string replacing?

\<!–StartFragment –\>

`Desktop\> 'aaa'.Replace('aaa', 'bbb', 'OrdinalIgnoreCase')` `Cannot find an overload for "Replace" and the argument count: "3".` `At line:1 char:1` `+ 'aaa'.Replace('aaa', 'bbb', 'OrdinalIgnoreCase')` `+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ` `+ CategoryInfo : NotSpecified: (:) [], MethodException` `+ FullyQualifiedErrorId : MethodCountCouldNotFindBest`

in C# the code works:

`foreach (string badString in badStrings)` ` fileName = fileName.Replace($".{badString}.", ".", StringComparison.OrdinalIgnoreCase);`

edit:

I found this workaround:

` foreach ($badString in $badStrings)`

` {`

` $badString = [Regex]::Escape($badString)`

` $fileName = $fileName -replace ('\.' + $badString + '\.'), '.'`

` }`

\<!--EndFragment --\>

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [January 25, 2020, 3:13pm UTC](https://forums.powershell.org/t/how-to-replace-string-case-insensitive/13710/2 "2020-01-25T15:13:22Z")

</div>

```
# Case-Insensitive
PS> 'AAAaaaBBBCDeFg' -replace 'a','*'
****** BBBCDeFg
# Case-Sensitive
PS> 'AAAaaaBBBCDeFg' -creplace 'a','*'
AAA***BBBCDeFg
```

---

<div class="post-metadata">

**Author:** ![stax76](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@stax76](https://forums.powershell.org/u/stax76)\
**Post date:** [January 25, 2020, 3:41pm UTC](https://forums.powershell.org/t/how-to-replace-string-case-insensitive/13710/3 "2020-01-25T15:41:27Z")

</div>

Thanks for the reply, the -replace operator uses regex which is problematic because my input strings might contain regex syntax.

I found my problem finally, my C# code is .NET Core based and apparently this overload does not exist in .NET Framework based PowerShell. I can use [Regex]::Escape to escape my input strings.

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [January 26, 2020, 8:53am UTC](https://forums.powershell.org/t/how-to-replace-string-case-insensitive/13710/4 "2020-01-26T08:53:47Z")

</div>

```
'Hi'.replace('h','i')
Hi

'Hi'.replace('H','i')
ii

'hi'.replace('.','j')
hi
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:30pm UTC](https://forums.powershell.org/t/how-to-replace-string-case-insensitive/13710/5 "2024-05-16T20:30:45Z")

</div>

Closed on May 16, 2024, 8:30pm UTC


