Search code examples
powershelldata-partitioning

Powershell random team generator with CSV file


I'm working with a script that should import my csv file, and randomly put 3 persons in a team, and make as many teams as there are players.

I can import my csv file, but i'm very lost on how to process from this point.

I know it's not alot, but I need help to get going.

$users = import-csv "C:\Users\Bruger\Dokumenter\Esport_liste.csv"
Write-Host $users

My CSV look like this: enter image description here


Solution

  • A possible solution would be:

    $users = import-csv "C:\Users\Bruger\Dokumenter\Esport_liste.csv" -Header "User"
    
    $i = 0
    $team = 1
    $users | Sort-Object { Get-Random } | foreach {
    
        if(($i % 3) -eq 0){ " "; "Team $($team)"; $team++ }
        $_.User
        $i++
    }
    

    I won't explain it further. Try to understand it and ask in the comments, if further help is needed.