Exporting individual files based on Import File

This topic contains 1 reply, has 2 voices, and was last updated by  James Crompton 1 month ago.

  • Author
    Posts
  • #102623

    Mike
    Participant

    Import File

    username
    john peters
    steve jobs
    bill gates
    

    Export files:
    john peters.csv

    username      name
    john peters   group1
    john peters   group2
    

    Steve jobs.csv

    steve jobs   group5
    steve jobs   group6
    

    And so on...

    The code I have so far will bring back the users and permissions in two columns. But I want them individually like above.

    E.g.

    username     name
    steve jobs   group1
    steve jobs   group2
    bill gates   group1
    bill gates   group2
    bill gates   group3
    

    Code so far:

    Import-Csv C:\Users.csv |
      ForEach-Object -pv user { Get-AdUser -filter "displayname -eq '$($_.username)'"} |
        Get-ADprincipalGroupMembership |
          Select-Object @{ n = 'samaccountname'; e = { $user.samaccountname } }, name |
            Export-csv -path C:\UserPermiss.csv -NoTypeInformation
    

    Any ideas on how to export the list of users & groups individually from 1 import file?

    Cheers!

  • #102626

    James Crompton
    Participant

    If i understand correctly your goal is to create many csv files as output, one per user with the groups listed?

    You should use the Group-Object cmdlet to do this. I don't have your exact case but i can give you a similar example with objects from Get-Process:

    get-process | Group-Object Name |% {$_.Group | Export-CSV -Path "C:\temp\$($_.Name).csv" -NoTypeInformation}
    

    This has two parts, the Group-Object will combine all elements with the same name into groups, then the |% (foreach) will loop through each group and pipe the contents of the group ($_.Group) out to Export-CSV so you will get one csv per group. the name of the csv in my case included the name of the group $_.Name.

      ForEach-Object -pv user { Get-AdUser -filter "displayname -eq '$($_.username)'"} |
        Get-ADprincipalGroupMembership |
          Select-Object @{ n = 'samaccountname'; e = { $user.samaccountname } }, name | 
            Group-Object username |% { $_.Group |Export-csv -path C:\UserPermiss_$($_.Name).csv -NoTypeInformation }
    

You must be logged in to reply to this topic.