Skip to content

Commit b7efbc4

Browse files
(AB-538621) Extend Write-Host note for non-primitive types
1 parent 4dde946 commit b7efbc4

File tree

4 files changed

+95
-15
lines changed

4 files changed

+95
-15
lines changed

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

Lines changed: 24 additions & 4 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
@@ -35,7 +35,7 @@ a string to use to separate displayed objects. The particular result depends on
3535
hosting PowerShell.
3636

3737
> [!NOTE]
38-
> Starting in Windows PowerShell 5.0, `Write-Host` is a wrapper for `Write-Information` This allows
38+
> Starting in Windows PowerShell 5.0, `Write-Host` is a wrapper for `Write-Information`. This allows
3939
> you to use `Write-Host` to emit output to the information stream. This enables the **capture** or
4040
> **suppression** of data written using `Write-Host` while preserving backwards compatibility.
4141
>
@@ -261,8 +261,28 @@ cmdlet sends to it.
261261
separated by a single space. This can be overridden with the **Separator** parameter.
262262

263263
- 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.
264+
provide meaningful output. For example, `@{a = 1; b = 2} | Write-Host` will print
265+
`System.Collections.Hashtable` to the host.
266+
267+
To work around this issue, you can manually create the string format you need with either string
268+
interpolation or the format operator (`-f`). You can also pass the object to the
269+
[Out-String](Out-String.md) command before passing it to `Write-Host`. The following snippet
270+
shows examples of each approach:
271+
272+
```powershell
273+
$ht = @{a = 1; b = 2}
274+
# String interpolation
275+
$ht.Keys.ForEach({ "[$_, $($ht[$_])]" }) -join ' ' | Write-Host
276+
# Format operator
277+
"[{0}, {1}] [{2}, {3}]" -f @('a', $ht.a, 'b', $ht.b) | Write-Host
278+
# Out-String
279+
$ht | Out-String | Write-Host -NoNewline
280+
```
281+
282+
For more information about string interpolation, see the article
283+
[Everything you wanted to know about variable substitution in strings](/powershell/scripting/learn/deep-dives/everything-about-string-substitutions).
284+
For more information about the format operator, see
285+
[about_Operators](../Microsoft.PowerShell.Core/About/about_Operators.md#format-operator--f).
266286

267287
## RELATED LINKS
268288

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

Lines changed: 23 additions & 3 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
@@ -261,8 +261,28 @@ cmdlet sends to it.
261261
separated by a single space. This can be overridden with the **Separator** parameter.
262262

263263
- 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.
264+
provide meaningful output. For example, `@{a = 1; b = 2} | Write-Host` will print
265+
`System.Collections.Hashtable` to the host.
266+
267+
To work around this issue, you can manually create the string format you need with either string
268+
interpolation or the format operator (`-f`). You can also pass the object to the
269+
[Out-String](Out-String.md) command before passing it to `Write-Host`. The following snippet
270+
shows examples of each approach:
271+
272+
```powershell
273+
$ht = @{a = 1; b = 2}
274+
# String interpolation
275+
$ht.Keys.ForEach({ "[$_, $($ht[$_])]" }) -join ' ' | Write-Host
276+
# Format operator
277+
"[{0}, {1}] [{2}, {3}]" -f @('a', $ht.a, 'b', $ht.b) | Write-Host
278+
# Out-String
279+
$ht | Out-String | Write-Host -NoNewline
280+
```
281+
282+
For more information about string interpolation, see the article
283+
[Everything you wanted to know about variable substitution in strings](/powershell/scripting/learn/deep-dives/everything-about-string-substitutions).
284+
For more information about the format operator, see
285+
[about_Operators](../Microsoft.PowerShell.Core/About/about_Operators.md#format-operator--f).
266286

267287
## RELATED LINKS
268288

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

Lines changed: 24 additions & 4 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
@@ -35,7 +35,7 @@ a string to use to separate displayed objects. The particular result depends on
3535
hosting PowerShell.
3636

3737
> [!NOTE]
38-
> Starting in Windows PowerShell 5.0, `Write-Host` is a wrapper for `Write-Information` This allows
38+
> Starting in Windows PowerShell 5.0, `Write-Host` is a wrapper for `Write-Information`. This allows
3939
> you to use `Write-Host` to emit output to the information stream. This enables the **capture** or
4040
> **suppression** of data written using `Write-Host` while preserving backwards compatibility.
4141
>
@@ -261,8 +261,28 @@ cmdlet sends to it.
261261
separated by a single space. This can be overridden with the **Separator** parameter.
262262

263263
- 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.
264+
provide meaningful output. For example, `@{a = 1; b = 2} | Write-Host` will print
265+
`System.Collections.Hashtable` to the host.
266+
267+
To work around this issue, you can manually create the string format you need with either string
268+
interpolation or the format operator (`-f`). You can also pass the object to the
269+
[Out-String](Out-String.md) command before passing it to `Write-Host`. The following snippet
270+
shows examples of each approach:
271+
272+
```powershell
273+
$ht = @{a = 1; b = 2}
274+
# String interpolation
275+
$ht.Keys.ForEach({ "[$_, $($ht[$_])]" }) -join ' ' | Write-Host
276+
# Format operator
277+
"[{0}, {1}] [{2}, {3}]" -f @('a', $ht.a, 'b', $ht.b) | Write-Host
278+
# Out-String
279+
$ht | Out-String | Write-Host -NoNewline
280+
```
281+
282+
For more information about string interpolation, see the article
283+
[Everything you wanted to know about variable substitution in strings](/powershell/scripting/learn/deep-dives/everything-about-string-substitutions).
284+
For more information about the format operator, see
285+
[about_Operators](../Microsoft.PowerShell.Core/About/about_Operators.md#format-operator--f).
266286

267287
## RELATED LINKS
268288

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

Lines changed: 24 additions & 4 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.6&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Write-Host
@@ -35,7 +35,7 @@ a string to use to separate displayed objects. The particular result depends on
3535
hosting PowerShell.
3636

3737
> [!NOTE]
38-
> Starting in Windows PowerShell 5.0, `Write-Host` is a wrapper for `Write-Information` This allows
38+
> Starting in Windows PowerShell 5.0, `Write-Host` is a wrapper for `Write-Information`. This allows
3939
> you to use `Write-Host` to emit output to the information stream. This enables the **capture** or
4040
> **suppression** of data written using `Write-Host` while preserving backwards compatibility.
4141
>
@@ -261,8 +261,28 @@ cmdlet sends to it.
261261
separated by a single space. This can be overridden with the **Separator** parameter.
262262

263263
- 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.
264+
provide meaningful output. For example, `@{a = 1; b = 2} | Write-Host` will print
265+
`System.Collections.Hashtable` to the host.
266+
267+
To work around this issue, you can manually create the string format you need with either string
268+
interpolation or the format operator (`-f`). You can also pass the object to the
269+
[Out-String](Out-String.md) command before passing it to `Write-Host`. The following snippet
270+
shows examples of each approach:
271+
272+
```powershell
273+
$ht = @{a = 1; b = 2}
274+
# String interpolation
275+
$ht.Keys.ForEach({ "[$_, $($ht[$_])]" }) -join ' ' | Write-Host
276+
# Format operator
277+
"[{0}, {1}] [{2}, {3}]" -f @('a', $ht.a, 'b', $ht.b) | Write-Host
278+
# Out-String
279+
$ht | Out-String | Write-Host -NoNewline
280+
```
281+
282+
For more information about string interpolation, see the article
283+
[Everything you wanted to know about variable substitution in strings](/powershell/scripting/learn/deep-dives/everything-about-string-substitutions).
284+
For more information about the format operator, see
285+
[about_Operators](../Microsoft.PowerShell.Core/About/about_Operators.md#format-operator--f).
266286

267287
## RELATED LINKS
268288

0 commit comments

Comments
 (0)