3

I've checked and tried a few of the suggestions on StackOverflow but none of them seem to work. I put together an example of what I am trying to accomplish.

[System.Random] $rand = New-Object System.Random
$randomNumbers = New-Object int[] 10;
[int[]] $randomNumbers;
for($i = 0; $i -lt $randomNumbers.Length; $i++)
{
    ($randomNumbers[$i] = $rand.Next(256)) 2>&1 | Out-Null;
}

I've tried the

> $Null
|Out-Null
2>&1

But none of them seem to suppress the output. It's showing 10 zero's in a row. One for each assignment. How can I suppress that output?

3 Answers 3

5

Remove int[]] $randomNumbers;. It is not the assignment that is printed, but the empty array.

Sign up to request clarification or add additional context in comments.

2 Comments

Doh! Sorry for stupid mistake but thanks for the answer. I'll accept when timer allows. I hate these kind of stupid mistakes but really appreciate the quick answer that I've been banging my head against keyboard for 30 minutes.
I actually don't even need to suppress output. $randomNumbers[$i] = $rand.Next(256) works just fine. I thought that was what was outputting all the zeros
2

other solution for replace your code ;)

 [int[]] $randomNumbers=1..10 | %{ Get-Random -maximum 256 }

2 Comments

++; I was busy crafting my answer with essentially the same idea while you posted this. Note that -Minimum should be 0 (and can therefore be omitted) to match the behavior of Random.Next Method (Int32)
then "Get-Random -maximum 256 " because 0 is default of minimum ;) i have modified my code
1

To complement Andrey Marchuk's effective answer:

  • [int[]] $randomNumbers looks like a type-bound PowerShell variable declaration, but, since no value is assigned, it is merely a cast: the preexisting value of $randomNumbers - a 10-element array of 0 values - is simply cast to [int[]] (a no-op in this case), and then output - yielding 10 lines with a 0 on each in this case.

    • A true type-bound assignment that is the (inefficient) equivalent of your New-Object int[] 10 statement is [int[]] $randomNumbers = @( 0 ) * 10.
      Note that it is the presence of = <value> that makes this statement an assignment that implicitly creates the variable.
    • PowerShell has no variable declarations in the conventional sense, it creates variables on demand when you assign to them.
      You can, however, use the New-Variable cmdlet to explicitly create variables, which allows you to control additional aspects, such as the variable's scope.
  • Variable assignments in PowerShell do NOT output anything by default, so there's no need to suppress any output (with | Out-Null, >$null, ...).

  • That said, you can force a variable assignment to output the assigned value by enclosing the assignment in (...).

    • $v = 'foo' # no output
    • ($v = 'foo') # enclosed in () -> 'foo' is output

As you've discovered, actively suppressing the output in ($randomNumbers[$i] = $rand.Next(256)) 2>&1 | Out-Null; is unnecessary, because simply omitting the parentheses makes the statement quiet: $randomNumbers[$i] = $rand.Next(256)


Finally, you could simplify your code using the Get-Random cmdlet:

[int[]] $randomNumbers = 1..10 | % { Get-Random -Maximum 256 } 

This single pipeline does everything your code does (not sure about performance, but it may not matter).

1 Comment

Not really worried about performance. I was more tinkering around while I learn more about PowerShell. Thanks for your description on variable declaration. I was struggling with declaring the array which is probably why I still had that extra line in there that was unneeded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.