0

I am trying to initialize arrays for each unique department in a CSV file. So far I have this and it doesn't seem to be working as intended.

$Org = Import-Csv Org.csv

$Dept_Unique = $Org.Department | Select-Object -Unique

This gives me the correct output and will list all the unique values of all departments in the CSV.

Accounting Team
Human Resources
Information Technology

Then I try to initialize an array for each of the departments with just some random values.

$Dept_Unique | ForEach-Object {
     $_ = @('User1','User2','User3')
}

My expected output would be...

${Human Resources}[1]
User2

However, I'm getting Cannot index into a null array.. Which tells me that array is not initializing how I want it to. Any help would be greatly appreciated, thanks!

5
  • Add another foreach to iterate through your users array. Commented May 14, 2021 at 17:02
  • 1
    You'd have to use the Add-Member cmdlet to do this. What's your real goal though. Commented May 14, 2021 at 17:02
  • Sounds like you want $Org |Group-Object Commented May 14, 2021 at 17:09
  • @AbrahamZinala can you please show me a sample? I'm not quite understanding where to put it. Commented May 14, 2021 at 17:10
  • @FoxDeploy My CSV has all USERS and all their DEPT, I want to build and array where USERS are added to their respective DEPT array Commented May 14, 2021 at 17:11

1 Answer 1

2

If you want to create a new variable named after a value stored in a different variable, use New-Variable:

$Dept_Unique | ForEach-Object {
    New-Variable -Name $_ -Value @('User1', 'User2', 'User3')
}

... but I suspect what you really want is just to group all user names based on department - for this you'll want to use the Group-Object cmdlet instead of Select-Object -Unique (the following assumes you have a Username column in your csv):

$Org |Group-Object Department |Select-Object @{Name='Dept';Expression='Name'},@{Name='Users';Expression={ @($_.Group.Username) }}

This will create one object per unique Department value, with two properties: Dept (the department name) and Users, an array of usernames associated with the department

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

4 Comments

Hi Mathias, appreciate your response, and the latter is exactly what I wanted. However, it is changing up my logic a bit and I'm a bit confused. Now that I have the Department and Users sorted accordingly. I want to write it to a text file for each department. Example ``` group_id = Human Resources group_members = User1, User2 group_id = Information Technology group_members = User3 ``` Thanks.
@script_newbie129 that can probably be done, but I don't know what data is in your csv - please update your question with a few sample lines (or at least the header) of the csv file, and describe exactly what output format you're aiming for
Hi Mathias, I was able to figure it out! I used your one liner and put that into another variable, then called upon the Dept/Users to write it into a text file. Then I used this: $myArray = "file1.csv","file2.csv" # Solution with single quote $a = "'$($myArray -join "','")'" $a # Result = 'file1.csv','file2.csv' # Solution with double quotes $b = '"{0}"' -f ($myArray -join '","') $b # Result = "file1.csv","file2.csv" To help me insert it the way I needed. Thank you very much!
@script_newbie129 If my answer solved your problem, please consider marking it "accepted" by clicking the checkmark on the left. If my answer is insufficient, feel free to post your own answer below (and mark that accepted so the question isn't listed as "unanswered") :-)

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.