-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
Steps to reproduce
There are three variations of the Replacement Operator:
-replaceis implicitly case-insensitive.-ireplaceis explicitly case-insensitive.-creplaceis case-sensitive.
The exception message returned when passing three or more comma separated values to -replace, -ireplace or -creplace is counterintuitive:
| Command | Exception Message |
|---|---|
"foo" -replace "o", "e", "e" |
InvalidOperation: The -ireplace operator allows only two elements to follow it, not 3. |
"foo" -ireplace "o", "e", "e" |
InvalidOperation: The -ireplace operator allows only two elements to follow it, not 3. |
"foo" -creplace "o", "e", "e" |
InvalidOperation: The -replace operator allows only two elements to follow it, not 3. |
As the table above demonstrates, when either the -replace or -ireplace operator is used, the message content refers to -ireplace. This is not ideal but since -replace and -ireplace are functionally equivalent it's perhaps not a big deal. But what is particularly inappropriate is that the exception message for -creplace refers to -replace.
Expected behavior
The exception message should ideally refer to the respective operator that was used. Else at a minimum, the message returned for the -creplace operator should refer to -creplace.
Environment data
The same result is exhibited in PowerShell 5.1, 7.0.0 and 7.1.0-preview.1, full version details below:
Name Value
---- -----
PSVersion 5.1.18362.628
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.18362.628
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Name Value
---- -----
PSVersion 7.0.0
PSEdition Core
GitCommitId 7.0.0
OS Microsoft Windows 10.0.18363
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Name Value
---- -----
PSVersion 7.1.0-preview.1
PSEdition Core
GitCommitId 7.1.0-preview.1
OS Microsoft Windows 10.0.18363
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0