Pick at least 1 random name from each department and put them in a list

Hey all,

I have a CSV that has a list of departments and associated names, I am trying to pick at least one random name from each department and put the picked names and departments in another list. I have done this with a do while loop but it is extremely slow, what is a more efficient way of doing it please?

Thanks very much

 

It would be nice if you can share us the code here, a different eye can point out the reason for the slowness :slight_smile:

[quote quote=254237]It would be nice if you can share us the code here, a different eye can point out the reason for the slowness :slightly_smiling_face:

[/quote]
Ahh I am sorry, here it is:

[pre]
$users = import-csv C:\Output.csv
$UniqueDepts = $Users.Department | Sort-Object -Unique

foreach ($department in $UniqueDepts) {

$count = 0
do {
    $RandomUser = Get-Random $users 
    if ($RandomUser.Department -eq $department) {
        $count++
        write-host $RandomUser.Department $RandomUser.SamAccountName # needs to go in a pscustomobject for output to csv
    }
} while ($count -le 1)

}[/pre]

Not sure how many records,but here is something to try:

$users = @"
Name,Department
John,Sales
Sally,Marketing
Frank,IT
Tom,IT
Bill,Sales
Alex,Sales
Samantha,Marketing
Alice,HR
"@ | ConvertFrom-Csv


$depts = $users | Group-Object -Property Department 
$depts | Select Name,
                @{Name='RandomUser';Expression={Get-Random $_.Group.Name -Count 1}}

Output:

PS C:\Users\rasim> $depts

Count Name                      Group                                                                                                                                                                                                    
----- ----                      -----                                                                                                                                                                                                    
    3 Sales                     {@{Name=John; Department=Sales}, @{Name=Bill; Department=Sales}, @{Name=Alex; Department=Sales}}                                                                                                         
    2 Marketing                 {@{Name=Sally; Department=Marketing}, @{Name=Samantha; Department=Marketing}}                                                                                                                            
    2 IT                        {@{Name=Frank; Department=IT}, @{Name=Tom; Department=IT}}                                                                                                                                               
    1 HR                        {@{Name=Alice; Department=HR}}                                                                                                                                                                           



PS C:\Users\rasim> $depts | Select Name,
                @{Name='RandomUser';Expression={Get-Random $_.Group.Name -Count 1}}

Name      RandomUser
----      ----------
Sales     John      
Marketing Sally     
IT        Frank     
HR        Alice     



PS C:\Users\rasim> $depts | Select Name,
                @{Name='RandomUser';Expression={Get-Random $_.Group.Name -Count 1}}

Name      RandomUser
----      ----------
Sales     John      
Marketing Sally     
IT        Tom       
HR        Alice     



PS C:\Users\rasim> $depts | Select Name,
                @{Name='RandomUser';Expression={Get-Random $_.Group.Name -Count 1}}

Name      RandomUser
----      ----------
Sales     Bill      
Marketing Sally     
IT        Frank     
HR        Alice     



PS C:\Users\rasim> $depts | Select Name,
                @{Name='RandomUser';Expression={Get-Random $_.Group.Name -Count 1}}

Name      RandomUser
----      ----------
Sales     Bill      
Marketing Samantha  
IT        Frank     
HR        Alice     

Thanks very much Rob - I will give that a go, it is around 4000 records, I take it I can Import-Csv | ConvertFrom-CSV to produce the key/value pairs?