How is it possible to import 2 csv's and pipe each output to the other??

Welcome Forums General PowerShell Q&A How is it possible to import 2 csv's and pipe each output to the other??

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
1 year, 6 months ago.

  • Author
    Posts
  • #70549
    Del

    Participant
    Points: 0
    Rank: Member

    Easy example :
    I have a list of users and I want all of them to be members of all of the security groups on the other ADGroups List
    $users = Import-CSV -Path $path
    $Groups = Import-CSV -Path $path

    Then Foreach($User in $Users) {Add-GroupMember -Identity "The Output from ADGroupList" -Members $User}

  • #70550

    Participant
    Points: 0
    Rank: Member

    Assuming that you are using two different CSV files, one containing users and one containing groups, you can add all users to all groups (assuming you have the appropriate AD permissions) with the following:

    $Users = Import-Csv -Path ...Users.csv
    $Groups = Import-Csv -Path ...Groups.csv
    
    foreach ($group in $Groups) {
    
        foreach ($user in $Users) {
        
            Add-ADGroupMember -Identity $group -Members $user
    
        }
    }
    

    Another approach I believe would work would be to take all your user names from your users file and concatenate them into a comma-separated string, e.g. $userNames = "user1","user2",....... and then you could use the following instead:

    # Import users and groups and create string of of comma-seperated user names and store in $userNames
    foreach ($group in $Groups) {
    
        Add-ADGroupMember -Identity $group -Members $userNames
    
    }
    

    If you are using one CSV with different columns for users and groups, I would need more information about the CSV file layout and then some small modifications would need to be made.

The topic ‘How is it possible to import 2 csv's and pipe each output to the other??’ is closed to new replies.