Skip to content

Fix Write-Host for IDictionary objects#24676

Open
brendandburns wants to merge 2 commits intoPowerShell:masterfrom
brendandburns:master
Open

Fix Write-Host for IDictionary objects#24676
brendandburns wants to merge 2 commits intoPowerShell:masterfrom
brendandburns:master

Conversation

@brendandburns
Copy link
Copy Markdown
Contributor

PR Summary

Fix Write-Host to consistently serialize using ToString, fixed #24671

PR Context

In the fixing of a bug related to XMLNode, IDictionary was also called out as an example of inconsistency. Following discussion in #24669 this code was also refactored to use switch instead of if .. else

PR Checklist

@daxian-dbw
Copy link
Copy Markdown
Member

This PR will change the current way of rendering for Write-Host $dictionary, which is a behavior change. We will need the "Cmdlet Working Group" to review #24671 and decide what behavior change is desired.

@daxian-dbw daxian-dbw added the WG-NeedsReview Needs a review by the labeled Working Group label Dec 13, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Dec 20, 2024
@{ Name = '-NoNewline:$true and colors'; Command = "Write-Host a,b -NoNewline:`$true -ForegroundColor Red -BackgroundColor Green;Write-Host a,b"; returnCount = 2; returnValue = @("Red:Green:a b:NoNewLine", "White:Black:a b:NewLine"); returnInfo = @("a b", "a b") }
@{ Name = '-NoNewline:$false and colors'; Command = "Write-Host a,b -NoNewline:`$false -ForegroundColor Red -BackgroundColor Green;Write-Host a,b"; returnCount = 2; returnValue = @("Red:Green:a b:NewLine","White:Black:a b:NewLine"); returnInfo = @("a b", "a b") }
@{ Name = 'XMLElement'; Command = "Write-Host ([xml] '<OhElement>Where art thou?</OhElement>').DocumentElement"; returnCount = 1; returnValue = @("White:Black:OhElement:NewLine"); returnInfo = @("OhElement") }
@{ Name = 'XMLDocument'; Command = "Write-Host ([system.xml.xmldocument] '<OhElement>Where art thou?</OhElement>').DocumentElement"; returnCount = 1; returnValue = @("White:Black:OhElement:NewLine"); returnInfo = @("OhElement") }
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a duplicate of this one.

@SteveL-MSFT SteveL-MSFT added the WG-Cmdlets general cmdlet issues label Jan 15, 2025
@SteveL-MSFT
Copy link
Copy Markdown
Member

The @PowerShell/wg-powershell-cmdlets reviewed this and we agree that this PR addresses an inconsistent behavior with Write-Host and IDictionary objects to be consistent with other IEnumerable types where the current behavior of emitting the type information is not useful.

@SteveL-MSFT SteveL-MSFT added WG-Reviewed A Working Group has reviewed this and made a recommendation and removed WG-NeedsReview Needs a review by the labeled Working Group labels Jan 15, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot removed the Review - Needed The PR is being reviewed label Jan 15, 2025
@microsoft-github-policy-service
Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Jan 23, 2025
Copilot AI review requested due to automatic review settings April 1, 2026 20:49
@brendandburns brendandburns requested a review from a team as a code owner April 1, 2026 20:49
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Write-Host’s stringification logic so IDictionary inputs are rendered consistently (using ToString()), addressing the inconsistency reported in #24671 and refactoring the object-type handling to a switch pattern match.

Changes:

  • Refactor WriteHostCommand.ProcessObject from if/else to switch pattern matching.
  • Special-case IDictionary so it’s not treated as an IEnumerable for unrolling, and is instead rendered via ToString().
  • Add a CI test case asserting the new IDictionary rendering behavior for Write-Host @{...}.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Microsoft.PowerShell.Commands.Utility/commands/utility/WriteConsoleCmdlet.cs Adjusts Write-Host object rendering to treat IDictionary as a scalar (use ToString()), and refactors to a switch.
test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Host.Tests.ps1 Adds a test case for IDictionary output formatting (type name via ToString()).

Comment on lines 63 to +65
@{ Name = 'XMLElement'; Command = "Write-Host ([xml] '<OhElement>Where art thou?</OhElement>').DocumentElement"; returnCount = 1; returnValue = @("White:Black:OhElement:NewLine"); returnInfo = @("OhElement") }
@{ Name = 'XMLDocument'; Command = "Write-Host ([system.xml.xmldocument] '<OhElement>Where art thou?</OhElement>').DocumentElement"; returnCount = 1; returnValue = @("White:Black:OhElement:NewLine"); returnInfo = @("OhElement") }
@{ Name = 'XMLDocument'; Command = "Write-Host ([system.xml.xmldocument] '<OhElement>Where art thou?</OhElement>').DocumentElement"; returnCount = 1; returnValue = @("White:Black:OhElement:NewLine"); returnInfo = @("OhElement") }
@{ Name = 'IDictionary'; Command = "Write-Host @{Key1='Value1';Key2='Value2'}"; returnCount = 1; returnValue = @("White:Black:System.Collections.Hashtable:NewLine"); returnInfo = @("System.Collections.Hashtable") }
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new 'IDictionary' test case only covers argument-binding (e.g. Write-Host @{...}), but the reported regression is specifically about pipeline input (@{...} | Write-Host / [Dictionary[...]] piped). Please add a test case that exercises the pipeline form and asserts it matches the non-pipeline output, ideally using a System.Collections.Generic.Dictionary[string,string] to mirror the issue repro.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review - Needed The PR is being reviewed WG-Cmdlets general cmdlet issues WG-Reviewed A Working Group has reviewed this and made a recommendation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Write-Host renders IDictionary inconsistently depending on if the value is piped in

5 participants