Case-insensitive PowerShell replacement Case-insensitive PowerShell replacement powershell powershell

Case-insensitive PowerShell replacement


Call me pedantic but while nobody here was outright wrong, nobody provided the correct code for the final solution either.

You need to change this line:

$NullSessionPipes =  $NullSessionPipes.replace("browser", "")

to this:

$NullSessionPipes =  $NullSessionPipes -ireplace [regex]::Escape("browser"), ""

The strange [regex] text isn't strictly necessary as long as there are no regular expression characters (ex. *+[](), etc) in your string. But you're safer with it. This syntax works with variables too:

$NullSessionPipes =  $NullSessionPipes -ireplace [regex]::Escape($stringToReplace), $stringToReplaceItWith


NullSessionPipes is a multi-string value and the replace method (in addition of being case-sensitive) may fail if there's more than one string in it. You can use the -replace operator. By default, all comparison operators are case-insensitive. Case-sensitive operators starts with 'c', like: -creplace,-ceq, etc.

Operators that starts with 'i' are case-insensitive, like -ireplace,-ieq, and they are the same as -replace, -ieq.

See the about_Comparison_Operators for more information.


Use a regular expression replacement instead:

$RegExplorer =  Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters$NullSessionPipes = "$($RegExplorer.NullSessionPipes)"$NullSessionPipes  $NullSessionPipes = $NullSessionPipes -replace "browser", ""$NullSessionPipes