PowerShell TIP – .replace vs -replace

I have been writing PowerShell scripts simple to advanced from a long time but still sometimes I get amazed with things that I come across while reviewing the automations and scripts. These are smaill things but can cause big issues if awareness is not there.

We always consider Powershell as case insensitive but this scenario is different where it always behave as case insensitive. I, most of the times use -replace so have not came across it since last week.

Issue was reported in one of the automation where a team is not catching users with certain attributes in their department’s termination process PowerShell script.

Here is the issue:

.replace keyword is case sensitive

-replace is case insensitive unless you specifically state it to do so.

See example below:

Replacing ADMIN with .replace does not do anything as it is case sensitive but performing same operation using -replace replaces the ADMIN as that is case insensitive.

For doing the same thing with .replace, we need type exactly as it appears to replace it.

This is a small post but will help anyone facing a similar scenario that we troubleshooted for one of our teams.


Thanks for reading …

Tech Wizard




PowerShell Fast Track


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s