Skip to content

Commit 6850478

Browse files
(AB-538621) Extend Write-Host note for non-primitive types
1 parent 7540ad4 commit 6850478

4 files changed

Lines changed: 187 additions & 71 deletions

File tree

reference/5.1/Microsoft.PowerShell.Utility/Write-Host.md

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Utility
5-
ms.date: 09/26/2023
5+
ms.date: 04/01/2026
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/write-host?view=powershell-5.1&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Write-Host
@@ -24,18 +24,17 @@ Write-Host [[-Object] <Object>] [-NoNewline] [-Separator <Object>] [-ForegroundC
2424
## DESCRIPTION
2525

2626
The `Write-Host` cmdlet's primary purpose is to produce for-(host)-display-only output, such as
27-
printing colored text like when prompting the user for input in conjunction with
28-
[Read-Host](Read-Host.md). `Write-Host` uses the [ToString()](/dotnet/api/system.object.tostring)
29-
method to write the output. By contrast, to output data to the pipeline, use
30-
[Write-Output](Write-Output.md) or implicit output.
27+
printing colored text like when prompting the user for input in conjunction with [Read-Host][01].
28+
`Write-Host` uses the [ToString()][02] method to write the output. By contrast, to output data to
29+
the pipeline, use [Write-Output][03] or implicit output.
3130

3231
You can specify the color of text by using the `ForegroundColor` parameter, and you can specify the
3332
background color by using the `BackgroundColor` parameter. The Separator parameter lets you specify
3433
a string to use to separate displayed objects. The particular result depends on the program that is
3534
hosting PowerShell.
3635

3736
> [!NOTE]
38-
> Starting in Windows PowerShell 5.0, `Write-Host` is a wrapper for `Write-Information` This allows
37+
> Starting in Windows PowerShell 5.0, `Write-Host` is a wrapper for `Write-Information`. This allows
3938
> you to use `Write-Host` to emit output to the information stream. This enables the **capture** or
4039
> **suppression** of data written using `Write-Host` while preserving backwards compatibility.
4140
>
@@ -113,8 +112,7 @@ Write-Host "I won't print" 6> $null
113112
These commands effectively suppress output of the `Write-Host` cmdlet. The first one uses the
114113
`InformationAction` parameter with the `Ignore` Value to suppress output to the information stream.
115114
The second example redirects the information stream of the command to the `$null` variable and
116-
thereby suppresses it. For more information, see
117-
[about_Output_Streams](../Microsoft.PowerShell.Core/About/about_Output_Streams.md).
115+
thereby suppresses it. For more information, see [about_Output_Streams][04].
118116

119117
## PARAMETERS
120118

@@ -261,23 +259,54 @@ cmdlet sends to it.
261259
separated by a single space. This can be overridden with the **Separator** parameter.
262260

263261
- Non-primitive data types such as objects with properties can cause unexpected results and not
264-
provide meaningful output. For example, `Write-Host @{a = 1; b = 2}` will print
265-
`System.Collections.DictionaryEntry System.Collections.DictionaryEntry` to the host.
262+
provide meaningful output. For example, `@{a = 1; b = 2} | Write-Host` will print
263+
`System.Collections.HashTable` to the host.
264+
265+
To work around this issue, you can manually create the string format you need with either
266+
[string interpolation][05] or the [format operator][06]. You can also pass the object to the
267+
[Out-String][07] command before passing it to `Write-Host`. The following snippet shows examples
268+
of each approach:
269+
270+
```powershell
271+
$ht = @{a = 1; b = 2}
272+
# String interpolation
273+
$ht.Keys.ForEach({ "[$_, $($ht[$_])]" }) -join ' ' | Write-Host
274+
# Format operator
275+
"[{0}, {1}] [{2}, {3}]" -f @('a', $ht.a, 'b', $ht.b) | Write-Host
276+
# Out-String
277+
$ht | Out-String | Write-Host
278+
```
266279

267280
## RELATED LINKS
268281

269-
[Clear-Host](../Microsoft.PowerShell.Core/Clear-Host.md)
282+
[Clear-Host][08]
270283

271-
[Out-Host](../Microsoft.PowerShell.Core/Out-Host.md)
284+
[Out-Host][09]
272285

273-
[Write-Debug](Write-Debug.md)
286+
[Write-Debug][10]
274287

275-
[Write-Error](Write-Error.md)
288+
[Write-Error][11]
276289

277-
[Write-Output](Write-Output.md)
290+
[Write-Output][03]
278291

279-
[Write-Progress](Write-Progress.md)
292+
[Write-Progress][12]
280293

281-
[Write-Verbose](Write-Verbose.md)
294+
[Write-Verbose][13]
282295

283-
[Write-Warning](Write-Warning.md)
296+
[Write-Warning][14]
297+
298+
<!-- Link reference definitions -->
299+
[01]: Read-Host.md
300+
[02]: /dotnet/api/system.object.tostring
301+
[03]: Write-Output.md
302+
[04]: ../Microsoft.PowerShell.Core/About/about_Output_Streams.md
303+
[05]: /powershell/scripting/learn/deep-dives/everything-about-string-substitutions
304+
[06]: ../Microsoft.PowerShell.Core/About/about_Operators.md#format-operator--f
305+
[07]: Out-String.md
306+
[08]: ../Microsoft.PowerShell.Core/Clear-Host.md
307+
[09]: ../Microsoft.PowerShell.Core/Out-Host.md
308+
[10]: Write-Debug.md
309+
[11]: Write-Error.md
310+
[12]: Write-Progress.md
311+
[13]: Write-Verbose.md
312+
[14]: Write-Warning.md

reference/7.4/Microsoft.PowerShell.Utility/Write-Host.md

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Utility
5-
ms.date: 09/26/2023
5+
ms.date: 04/01/2026
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/write-host?view=powershell-7.4&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Write-Host
@@ -24,10 +24,9 @@ Write-Host [[-Object] <Object>] [-NoNewline] [-Separator <Object>] [-ForegroundC
2424
## DESCRIPTION
2525

2626
The `Write-Host` cmdlet's primary purpose is to produce for-(host)-display-only output, such as
27-
printing colored text like when prompting the user for input in conjunction with
28-
[Read-Host](Read-Host.md). `Write-Host` uses the [ToString()](/dotnet/api/system.object.tostring)
29-
method to write the output. By contrast, to output data to the pipeline, use
30-
[Write-Output](Write-Output.md) or implicit output.
27+
printing colored text like when prompting the user for input in conjunction with [Read-Host][01].
28+
`Write-Host` uses the [ToString()][02] method to write the output. By contrast, to output data to
29+
the pipeline, use [Write-Output][03] or implicit output.
3130

3231
You can specify the color of text by using the `ForegroundColor` parameter, and you can specify the
3332
background color by using the `BackgroundColor` parameter. The Separator parameter lets you specify
@@ -113,8 +112,7 @@ Write-Host "I won't print" 6> $null
113112
These commands effectively suppress output of the `Write-Host` cmdlet. The first one uses the
114113
`InformationAction` parameter with the `Ignore` Value to suppress output to the information stream.
115114
The second example redirects the information stream of the command to the `$null` variable and
116-
thereby suppresses it. For more information, see
117-
[about_Output_Streams](../Microsoft.PowerShell.Core/About/about_Output_Streams.md).
115+
thereby suppresses it. For more information, see [about_Output_Streams][04].
118116

119117
## PARAMETERS
120118

@@ -261,23 +259,54 @@ cmdlet sends to it.
261259
separated by a single space. This can be overridden with the **Separator** parameter.
262260

263261
- Non-primitive data types such as objects with properties can cause unexpected results and not
264-
provide meaningful output. For example, `Write-Host @{a = 1; b = 2}` will print
265-
`System.Collections.DictionaryEntry System.Collections.DictionaryEntry` to the host.
262+
provide meaningful output. For example, `@{a = 1; b = 2} | Write-Host` will print
263+
`System.Collections.HashTable` to the host.
264+
265+
To work around this issue, you can manually create the string format you need with either
266+
[string interpolation][05] or the [format operator][06]. You can also pass the object to the
267+
[Out-String][07] command before passing it to `Write-Host`. The following snippet shows examples
268+
of each approach:
269+
270+
```powershell
271+
$ht = @{a = 1; b = 2}
272+
# String interpolation
273+
$ht.Keys.ForEach({ "[$_, $($ht[$_])]" }) -join ' ' | Write-Host
274+
# Format operator
275+
"[{0}, {1}] [{2}, {3}]" -f @('a', $ht.a, 'b', $ht.b) | Write-Host
276+
# Out-String
277+
$ht | Out-String | Write-Host
278+
```
266279

267280
## RELATED LINKS
268281

269-
[Clear-Host](../Microsoft.PowerShell.Core/Clear-Host.md)
282+
[Clear-Host][08]
270283

271-
[Out-Host](../Microsoft.PowerShell.Core/Out-Host.md)
284+
[Out-Host][09]
272285

273-
[Write-Debug](Write-Debug.md)
286+
[Write-Debug][10]
274287

275-
[Write-Error](Write-Error.md)
288+
[Write-Error][11]
276289

277-
[Write-Output](Write-Output.md)
290+
[Write-Output][03]
278291

279-
[Write-Progress](Write-Progress.md)
292+
[Write-Progress][12]
280293

281-
[Write-Verbose](Write-Verbose.md)
294+
[Write-Verbose][13]
282295

283-
[Write-Warning](Write-Warning.md)
296+
[Write-Warning][14]
297+
298+
<!-- Link reference definitions -->
299+
[01]: Read-Host.md
300+
[02]: /dotnet/api/system.object.tostring
301+
[03]: Write-Output.md
302+
[04]: ../Microsoft.PowerShell.Core/About/about_Output_Streams.md
303+
[05]: /powershell/scripting/learn/deep-dives/everything-about-string-substitutions
304+
[06]: ../Microsoft.PowerShell.Core/About/about_Operators.md#format-operator--f
305+
[07]: Out-String.md
306+
[08]: ../Microsoft.PowerShell.Core/Clear-Host.md
307+
[09]: ../Microsoft.PowerShell.Core/Out-Host.md
308+
[10]: Write-Debug.md
309+
[11]: Write-Error.md
310+
[12]: Write-Progress.md
311+
[13]: Write-Verbose.md
312+
[14]: Write-Warning.md

reference/7.5/Microsoft.PowerShell.Utility/Write-Host.md

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Utility
5-
ms.date: 09/26/2023
5+
ms.date: 04/01/2026
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/write-host?view=powershell-7.5&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Write-Host
@@ -24,18 +24,17 @@ Write-Host [[-Object] <Object>] [-NoNewline] [-Separator <Object>] [-ForegroundC
2424
## DESCRIPTION
2525

2626
The `Write-Host` cmdlet's primary purpose is to produce for-(host)-display-only output, such as
27-
printing colored text like when prompting the user for input in conjunction with
28-
[Read-Host](Read-Host.md). `Write-Host` uses the [ToString()](/dotnet/api/system.object.tostring)
29-
method to write the output. By contrast, to output data to the pipeline, use
30-
[Write-Output](Write-Output.md) or implicit output.
27+
printing colored text like when prompting the user for input in conjunction with [Read-Host][01].
28+
`Write-Host` uses the [ToString()][02] method to write the output. By contrast, to output data to
29+
the pipeline, use [Write-Output][03] or implicit output.
3130

3231
You can specify the color of text by using the `ForegroundColor` parameter, and you can specify the
3332
background color by using the `BackgroundColor` parameter. The Separator parameter lets you specify
3433
a string to use to separate displayed objects. The particular result depends on the program that is
3534
hosting PowerShell.
3635

3736
> [!NOTE]
38-
> Starting in Windows PowerShell 5.0, `Write-Host` is a wrapper for `Write-Information` This allows
37+
> Starting in Windows PowerShell 5.0, `Write-Host` is a wrapper for `Write-Information`. This allows
3938
> you to use `Write-Host` to emit output to the information stream. This enables the **capture** or
4039
> **suppression** of data written using `Write-Host` while preserving backwards compatibility.
4140
>
@@ -113,8 +112,7 @@ Write-Host "I won't print" 6> $null
113112
These commands effectively suppress output of the `Write-Host` cmdlet. The first one uses the
114113
`InformationAction` parameter with the `Ignore` Value to suppress output to the information stream.
115114
The second example redirects the information stream of the command to the `$null` variable and
116-
thereby suppresses it. For more information, see
117-
[about_Output_Streams](../Microsoft.PowerShell.Core/About/about_Output_Streams.md).
115+
thereby suppresses it. For more information, see [about_Output_Streams][04].
118116

119117
## PARAMETERS
120118

@@ -261,23 +259,54 @@ cmdlet sends to it.
261259
separated by a single space. This can be overridden with the **Separator** parameter.
262260

263261
- Non-primitive data types such as objects with properties can cause unexpected results and not
264-
provide meaningful output. For example, `Write-Host @{a = 1; b = 2}` will print
265-
`System.Collections.DictionaryEntry System.Collections.DictionaryEntry` to the host.
262+
provide meaningful output. For example, `@{a = 1; b = 2} | Write-Host` will print
263+
`System.Collections.HashTable` to the host.
264+
265+
To work around this issue, you can manually create the string format you need with either
266+
[string interpolation][05] or the [format operator][06]. You can also pass the object to the
267+
[Out-String][07] command before passing it to `Write-Host`. The following snippet shows examples
268+
of each approach:
269+
270+
```powershell
271+
$ht = @{a = 1; b = 2}
272+
# String interpolation
273+
$ht.Keys.ForEach({ "[$_, $($ht[$_])]" }) -join ' ' | Write-Host
274+
# Format operator
275+
"[{0}, {1}] [{2}, {3}]" -f @('a', $ht.a, 'b', $ht.b) | Write-Host
276+
# Out-String
277+
$ht | Out-String | Write-Host
278+
```
266279

267280
## RELATED LINKS
268281

269-
[Clear-Host](../Microsoft.PowerShell.Core/Clear-Host.md)
282+
[Clear-Host][08]
270283

271-
[Out-Host](../Microsoft.PowerShell.Core/Out-Host.md)
284+
[Out-Host][09]
272285

273-
[Write-Debug](Write-Debug.md)
286+
[Write-Debug][10]
274287

275-
[Write-Error](Write-Error.md)
288+
[Write-Error][11]
276289

277-
[Write-Output](Write-Output.md)
290+
[Write-Output][03]
278291

279-
[Write-Progress](Write-Progress.md)
292+
[Write-Progress][12]
280293

281-
[Write-Verbose](Write-Verbose.md)
294+
[Write-Verbose][13]
282295

283-
[Write-Warning](Write-Warning.md)
296+
[Write-Warning][14]
297+
298+
<!-- Link reference definitions -->
299+
[01]: Read-Host.md
300+
[02]: /dotnet/api/system.object.tostring
301+
[03]: Write-Output.md
302+
[04]: ../Microsoft.PowerShell.Core/About/about_Output_Streams.md
303+
[05]: /powershell/scripting/learn/deep-dives/everything-about-string-substitutions
304+
[06]: ../Microsoft.PowerShell.Core/About/about_Operators.md#format-operator--f
305+
[07]: Out-String.md
306+
[08]: ../Microsoft.PowerShell.Core/Clear-Host.md
307+
[09]: ../Microsoft.PowerShell.Core/Out-Host.md
308+
[10]: Write-Debug.md
309+
[11]: Write-Error.md
310+
[12]: Write-Progress.md
311+
[13]: Write-Verbose.md
312+
[14]: Write-Warning.md

0 commit comments

Comments
 (0)