Powershell: Compare-Object: -ExcludeDifferent should imply -IncludeEqual

Created on 13 Feb 2020  路  2Comments  路  Source: PowerShell/PowerShell

Compare-Object only reports _differences_ by default, requiring explicit opt-in for inclusion of _equal_ objects via -IncludeEqual.

Therefore, the only meaningful behavior when you use -ExcludeDifferent is to _default_ to -IncludeEqual.

Without that, as is currently the case, _nothing_ is ever output, so it is currently effectively pointless to pass -ExcludeDifferent _without also passing -IncludeEqual_.

Steps to reproduce

Compare-Object 1,2 3, 1 -ExcludeDifferent -PassThru -IncludeEqual  | Should -Be 1
# Currently produces NO output, because -IncludeEqual isn't implied.
Compare-Object 1,2 3, 1 -ExcludeDifferent -PassThru | Should -Be 1

Expected behavior

Both tests should pass.

Actual behavior

The 2nd test fails, because -IncludeEqual is not implied.

Environment data

PowerShell Core 7.0.0-rc.2
Area-Cmdlets-Utility Issue-Question Resolution-Fixed Up-for-Grabs

Most helpful comment

It appears that it is already _supposed_ to do that, but clearly isn't. See Compare-Object.cs L374:

/// <summary>
/// If the parameter 'ExcludeDifferent' is present, then we need to turn on the
/// 'IncludeEqual' switch unless it's turned off by the user specifically.
/// </summary>
protected override void BeginProcessing()
{
    if (ExcludeDifferent)
    {
        if (_isIncludeEqualSpecified == false)
        {
            return;
        }

        if (_isIncludeEqualSpecified && !_includeEqual)
        {
            return;
        }

        _includeEqual = true;
    }
}

All 2 comments

It appears that it is already _supposed_ to do that, but clearly isn't. See Compare-Object.cs L374:

/// <summary>
/// If the parameter 'ExcludeDifferent' is present, then we need to turn on the
/// 'IncludeEqual' switch unless it's turned off by the user specifically.
/// </summary>
protected override void BeginProcessing()
{
    if (ExcludeDifferent)
    {
        if (_isIncludeEqualSpecified == false)
        {
            return;
        }

        if (_isIncludeEqualSpecified && !_includeEqual)
        {
            return;
        }

        _includeEqual = true;
    }
}

:tada:This issue was addressed in #12317, which has now been successfully released as v7.1.0-preview.3.:tada:

Handy links:

Was this page helpful?
0 / 5 - 0 ratings