0

I have 2 .csv files.

Examples:

1.csv:

Users
userA
userB

2.csv:

groupid,user,random
1,userA,randomtext1
2,userA,randomtest2
3,userA,randomtext3
3,userB,randomtext1
5,userB,randomtext2
6,userB,randomtext2

I want to take the value of the one column in 1.csv and check it against a column in 2.csv.

Basically what output I would like is:

UserA is a member of 1
UserA is a member of 2
UserA is a member of 3
UserB is a member of 3
UserB is a member of 4
UserB is a member of 5
UserB is a member of 6

I know I can use Import-Csv but I just can't get around how to do this?

I have tried to put 1.csv into an array but how can i then check the array against every line in 2.csv to check if the user is on a given line and then take the groupip?

$user = @()
$users = Import-Csv 1.csv | Foreach {$user += $_.Users}

I have tried a lot of different things and i cant get it to work.

Any tips appreciated.

/Rasmus

1 Answer 1

1

Read the usernames you're interested in into an array, use Where-Object to filter on rows that match those user names:

# Read user names into string array
$TargetUsers = Import-Csv .\1.csv |Select-Object -ExpandProperty Users

# Now import and filter the CSV with the relationships
Import-Csv .\2.csv |Where-Object {$TargetUsers -contain $_.user} |ForEach-Object {
    # Output the string with the information you need
    "$($_.user) is a member of $($_.groupid)"
} |Out-File .\output.txt
Sign up to request clarification or add additional context in comments.

Comments

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.