Skip to content

Commit d57272c

Browse files
davidsmatlaksdwheeler
authored andcommitted
Fixes #4859 - PS7 Get-Random InputObject parameter accepts null or empty string (#4862)
* issue 4859 * fixed Example 13 heading
1 parent c01fb9e commit d57272c

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

reference/7/Microsoft.PowerShell.Utility/Get-Random.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ online version: https://go.microsoft.com/fwlink/?linkid=2097016
88
schema: 2.0.0
99
title: Get-Random
1010
---
11+
1112
# Get-Random
1213

1314
## SYNOPSIS
@@ -148,7 +149,7 @@ yellow
148149

149150
This example shows the effect of using the **SetSeed** parameter.
150151

151-
Because **SetSeed** produces non-random behavior, it is typically used only to reproduce results,
152+
Because **SetSeed** produces non-random behavior, it's typically used only to reproduce results,
152153
such as when debugging or analyzing a script.
153154

154155
```powershell
@@ -241,6 +242,18 @@ Get-Random -Count 3 -Maximum 10
241242
8
242243
```
243244

245+
### Example 13: Use the InputObject parameter with an empty string or $null
246+
247+
In this example, the **InputObject** parameter specifies an array that contains an empty string
248+
(`''`) and `$null`.
249+
250+
```powershell
251+
Get-Random -InputObject @('a','',$null)
252+
```
253+
254+
`Get-Random` will return either `a`, empty string, or `$null`. The empty sting displays as a blank
255+
line and `$null` returns to a PowerShell prompt.
256+
244257
## PARAMETERS
245258

246259
### -Count
@@ -269,6 +282,9 @@ the collection up to the number specified by **Count**. Enter the objects, a var
269282
the objects, or a command or expression that gets the objects. You can also pipe a collection of
270283
objects to `Get-Random`.
271284

285+
Beginning in PowerShell 7, the **InputObject** parameter accepts arrays that can contain an empty
286+
string or `$null`. The array can be sent down the pipeline or as an **InputObject** parameter value.
287+
272288
```yaml
273289
Type: Object[]
274290
Parameter Sets: RandomListItemParameterSet
@@ -335,11 +351,10 @@ Accept wildcard characters: False
335351

336352
Specifies a seed value for the random number generator. This seed value is used for the current
337353
command and for all subsequent `Get-Random` commands in the current session until you use
338-
**SetSeed** again or close the session. You cannot reset the seed to its default value.
354+
**SetSeed** again or close the session. You can't reset the seed to its default value.
339355

340-
The **SetSeed** parameter is not required. By default, `Get-Random` uses the
341-
[RandomNumberGenerator()](/dotnet/api/system.security.cryptography.randomnumbergenerator)
342-
method to generate a seed value. Because **SetSeed** results in non-random behavior, it is typically
356+
The **SetSeed** parameter isn't required. By default, `Get-Random` uses the [RandomNumberGenerator()](/dotnet/api/system.security.cryptography.randomnumbergenerator)
357+
method to generate a seed value. Because **SetSeed** results in non-random behavior, it's typically
343358
used only when trying to reproduce behavior, such as when debugging or analyzing a script that
344359
includes `Get-Random` commands.
345360

@@ -382,4 +397,9 @@ starts.
382397
Beginning in Windows PowerShell 3.0, `Get-Random` supports 64-bit integers. In Windows PowerShell
383398
2.0, all values are cast to **System.Int32**.
384399

400+
Beginning in PowerShell 7, the **InputObject** parameter in the **RandomListItemParameterSet**
401+
parameter set accepts arrays that contain an empty string or `$null`. In earlier PowerShell
402+
versions, only the **Maximum** parameter in the **RandomNumberParameterSet** parameter set accepted
403+
an empty string or `$null`.
404+
385405
## RELATED LINKS

0 commit comments

Comments
 (0)