0

I need to create a set of permutations without repetition for an arbitrary number of strings in Powershell. The simplest example is:

$x = "Alpha","Beta","Charlie"

Output would be:

Alpha 
Beta 
Charlie 
Alpha Beta
Alpha Charlie
Beta Charlie

I haven't been able to find a useful example despite heavy Google and StackExchange searching.

Matt
  • 42,566
  • 8
  • 67
  • 104
Redblur
  • 19
  • 4

1 Answers1

-3

I'm betting there's a cleaner way to do this, but here I'm just pulling a random number for the array key and just looping it out verifying that new random number isn't the same as the last number.

$x = "Alpha","Beta","Charlie"

for($i=1; $i -le 10; $i++)
{
    while ($random -eq $lastnum)
    {
        [int]$random = Get-Random -Minimum 0 -Maximum $x.Count
    }
    $lastnum = $random

    Write-Host $x[$random]
}
Jeffrey Eldredge
  • 769
  • 6
  • 11