PowerShell/PowerShell · error · ArgumentOutOfRangeException
'minValue' cannot be greater than maxValue.
Error message
'minValue' cannot be greater than maxValue.
What it means
Thrown by the Get-Random cmdlet's internal generator method Next(int minValue, int maxValue) when the inclusive lower bound exceeds the exclusive upper bound. The contract is maxValue >= minValue; the method guards this before computing the random range and refuses to produce a nonsensical result.
Source
Thrown at src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommandBase.cs:649
if (maxValue < 0)
{
throw new ArgumentOutOfRangeException(nameof(maxValue), GetRandomCommandStrings.MaxMustBeGreaterThanZeroApi);
}
return Next(0, maxValue);
}
/// <summary>
/// Returns a random integer that is within a specified range.
/// </summary>
/// <param name="minValue">The inclusive lower bound of the random number returned.</param>
/// <param name="maxValue">The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.</param>
/// <returns>Next random integer.</returns>
public int Next(int minValue, int maxValue)
{
if (minValue > maxValue)
{
throw new ArgumentOutOfRangeException(nameof(minValue), GetRandomCommandStrings.MinGreaterThanOrEqualMaxApi);
}
int randomNumber = 0;
long range = (long)maxValue - (long)minValue;
if (range <= int.MaxValue)
{
randomNumber = (int)(NextDouble() * range) + minValue;
}
else
{
double largeSample = InternalSampleLargeRange() * (1.0 / (2 * ((uint)int.MaxValue)));
randomNumber = (int)((long)(largeSample * range) + minValue);
}
return randomNumber;
}
View on GitHub (pinned to 3ff3c711bf)
Solutions
- Ensure -Maximum is greater than -Minimum before calling Get-Random; swap if the caller's inputs are unordered
- Compute bounds defensively: int lo=Math.Min(a,b), hi=Math.Max(a,b) then use lo as min, hi as max
- Validate the range up front and surface a clear caller-side error rather than letting Get-Random throw
Example fix
// before Get-Random -Minimum 100 -Maximum 10 // after Get-Random -Minimum 10 -Maximum 100
Defensive patterns
Strategy: validation
Validate before calling
if ($Minimum -gt $Maximum) { throw "Minimum ($Minimum) must be <= Maximum ($Maximum)" }
Get-Random -Minimum $Minimum -Maximum $Maximum Type guard
null
Try / catch
null
Prevention
- Order caller-supplied bounds with [Math]::Min/[Math]::Max before passing them to Get-Random
- Validate ranges at the boundary of your script/function before they reach Get-Random
When it happens
Trigger: Calling Get-Random -Minimum N -Maximum M where N > M, or invoking the public Next(minValue,maxValue) API directly with minValue greater than maxValue (e.g. Next(10, 5)).
Common situations: Swapped -Minimum/-Maximum argument order, computing bounds from data where the 'max' value is unexpectedly smaller than the 'min' (empty range, off-by-one), or passing user-supplied range values without ordering them.
Related errors
- {argumentName}
- Argument '{0}' contains characters that are not allowed in p
- 'maxValue' must be greater than zero.
- Non negative number is required.
- The ID parameter must be in the range {0} through {1}.
AI-assisted analysis of PowerShell/PowerShell@3ff3c711bf (2026-08-13).
Data as JSON: /api/errors/e103fb0fdd9a8e5d.
Report an issue: GitHub.