Skip to content

Replace Operator InvalidOperation Exception Message #12333

@thecliguy

Description

@thecliguy

Steps to reproduce

There are three variations of the Replacement Operator:

  • -replace is implicitly case-insensitive.
  • -ireplace is explicitly case-insensitive.
  • -creplace is 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    First-Time-IssueEasy issues first time contributors can work on to learn about this projectIssue-Code Cleanupthe issue is for cleaning up the code with no impact on functionalityResolution-FixedThe issue is fixed.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions