Skip to content

Commit

Permalink
(MicrosoftDocsGH-10088) Add example for normalizing Read-Host input
Browse files Browse the repository at this point in the history
Prior to this change, the documentation for the `Read-Host` cmdlet didn't include
information about trimming the input. The cmdlet doesn't have a built-in way to
trim extra spaces from the input value, which is an arbitrary string.

This change:

- Adds an example to `Read-Host` showing how user input may need to normalized
  before it can be used. It also shows how a reader can apply some simple
  normalizing techniques to clean up the input.
- Resolves MicrosoftDocs#10088
- Fixes AB#91532
  • Loading branch information
michaeltlombardi committed May 22, 2023
1 parent ea377f3 commit f8fb80a
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion reference/7.3/Microsoft.PowerShell.Utility/Read-Host.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
Locale: en-US
Module Name: Microsoft.PowerShell.Utility
ms.date: 12/12/2022
ms.date: 05/22/2023
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/read-host?view=powershell-7.3&WT.mc_id=ps-gethelp
schema: 2.0.0
title: Read-Host
Expand Down Expand Up @@ -67,6 +67,56 @@ value is stored as a plaintext **String** object in the `$pwd_string` variable.
$pwd_string = Read-Host "Enter a Password" -MaskInput
```

### Example 4: Normalizing input

This example prompts the user to input a list of cities separated by semi-colons. It shows the
string's value as typed by the user. In the example, the user added spaces between some of
the entries. This could lead to an error later in the script where the code expects an exact
name.

The example shows how you can convert an input string into an array of entries without any
extra spaces.

```powershell
$prompt = @(
'List the cities you want weather information for.'
'When specifying multiple cities, separate them with a semi-colon, like:'
"'New York; Osan; Koforidua'"
) -join ' '
$cities = Read-Host $prompt
"Input cities string: `n`t'$cities'"
$splitCities = $cities -split ';'
"Split cities array:"
$splitCities | ForEach-Object -Process { "`t'$_'" }
$normalizedCities = $splitCities | ForEach-Object -Process { $_.Trim() }
"Normalized split cities array:"
$normalizedCities | ForEach-Object -Process { "`t'$_'" }
```

```output
Input cities string:
' New York; Osan ;Koforidua '
Split cities array:
' New York'
' Osan '
'Koforidua '
Normalized split cities array:
'New York'
'Osan'
'Koforidua'
```

The example uses the `-split` operator to convert the input string into an array of strings. Each
string in the array includes the name of a different city. However, the split strings still include
extra spaces. The example uses the **Trim** method on each string in the array to remove the
spaces.

## PARAMETERS

### -AsSecureString
Expand Down

0 comments on commit f8fb80a

Please sign in to comment.