Assistance required to add a new column to the output csv in this AD Group/User Script RRS feed

  • Question

  • Pls find shortened down and testable powershell code at bottom of this post. The task is to amend this script so it adds another column called 'Printer Groups'. This will list all the AD groups named like *Printer Access* that each user is a member of. Hopefully the screenshot below of the output shows what they are after :-)

    I know if I run this following line of code on its own it works but I need to combine it with the powershell script (provided further down below).

    Get-adgroup -Filter {name -like “*Printer Access*”} -Properties * | select name

    Please help any powershell scripting gurus/gods 🙂

    param (
        [string[]] $Groups = @("Administrators"),
        [string] $GroupsFile,
        [string] $Path
    # Failed group hashtable
    $FailedGroups = @{}
    #$Groups = Get-Content $GroupsFile -ErrorAction Stop
        $Output = foreach ($Group in $Groups) {
            # Whitespace and empty string check is to support loading from groups file
            $Group = $Group.Trim()
            if ($Group -ne "") {
                try {
                        Get-ADGroupMember -Identity $Group -Recursive | Get-ADUser -Properties DisplayName,GivenName,Surname,SamAccountName,Description,LastLogonDate,Enabled,PasswordNeverExpires,whenCreated,Title,Department,Manager,TelephoneNumber,MobilePhone,Office,EmployeeID -ErrorAction SilentlyContinue |
                        Select-Object -Property @{name = "Group Name"; expression = {$Group}},
                                                @{name = "Display Name"; expression = {$_.DisplayName}},
                                                @{name = "First Name"; expression = {$_.GivenName}},
                                                @{name = "Last Name"; expression = {$_.Surname}},
                                                @{name = "Username"; expression = {$_.SamAccountName}},                   
                                                @{name = "Last logon date"; expression = {$_.LastLogonDate}},
                                                @{name = "Password never expires"; expression = {$_.PasswordNeverExpires}},
                                                @{name = "Date created"; expression = {$_.whenCreated}},
                                                @{name = "Title"; expression = {$_.Title}},
                                                @{name = "Department"; expression = {$_.Department}},
                                                @{name = "Manager"; expression = {$_.Manager}},
                                                @{name = "TelephoneNumber"; expression = {$_.TelephoneNumber}},
                                                @{name = "MobilePhone"; expression = {$_.MobilePhone}},
                                                @{name = "Office"; expression = {$_.Office}},
                                                @{name = "EmployeeID"; expression = {$_.EmployeeID}}
                catch {
                    $FailedGroups[$Group] = $_.Exception.Message
        $Output | Export-Csv "C:\temp\test.csv" -NoTypeInformation -Encoding UTF8 -Force
        Write-Host "CSV file has been saved to C:\temp"

    Friday, July 31, 2020 3:59 AM

All replies

  • The group the user is a member of is already in the results.  Why do you need it again?

    If you are looking to find all of the groups for the user then you need to add "Get-AdUser" and get all of the group Distinguished names that match your requirement.

    You can learn how to do that here: Windows PowerShell™: TFM

    help Get-AdUser -online


    • Proposed as answer by jrv Friday, July 31, 2020 6:20 PM
    Friday, July 31, 2020 4:12 AM
  • Thankyou for the reply but its not in the results. 

    The script lists all users that are members of the 'Administrators' AD group. The output csv has the columns of data for all these users i.e. Display Name, First Name, Last Name etc etc We need to add one more column that tells us what 'Printer Access' AD groups each of the users is a member of. See the table example below (scroll to the right).

    Friday, July 31, 2020 4:21 AM
  • You have to rewrite the script to get what you want.  It requires more than just adding a column.  I gave =you the instructions above.

    This forum is not a place to ask for others to rewrite scripts you have found on the Internet.  It is for technicians that have learned basic PowerShell and have specific questions about a script they have written.

    Please read the post pinned at the top of this forum for the rules on what you can post and ask.


    Friday, July 31, 2020 4:30 AM
  • That was extremely rude but noted. You gave no instructions at all. It isnt a script off the internet either and I am a IT engineer. Just asking for some assistance. Clearly you dont understand the coding. Hopefully someone else will reply
    Friday, July 31, 2020 4:34 AM
  • You didn't write the script so you don't understand how to modify it.  DO you have a degree or certification in IT engineering?   Engineers would know how to proceed.

    I gave you the base bits you need to do this.  You have to use Get-AdUser and then expand all of the "memberof" property and filter for the groups you are interested in.  When acquired then format them as desired for output.

    This is a basic junior engineers problem.  Put your IT hat on and follow the instructions.  You can find hundreds of examples  on the Internet or you can use the following book to learn how to do this:

    Windows PowerShell™: TFM

    All of the instructions you need to write this script are in this book.  I cannot teach you this and I do not feel like taking 30 minutes to rewrite your script.


    • Proposed as answer by BOfH-666 Friday, July 31, 2020 7:34 AM
    • Edited by jrv Friday, July 31, 2020 7:36 AM
    Friday, July 31, 2020 5:30 AM
  • Ok - I have a bit of time so I will give you the recipe to follow to accomplish your task.

    1. Copy the last column spec and paste it back as a new column.
    2. Change the name of the new column to the name you want.
    3. Run the script to verify that the new column has the right name and is in the correct location.  This will test and debug your changes,
    4. Change the expression to get the user with Get-AdUser and output the user name  for now.
    5. Test again to see the correct users is being returned.
    6.  Retrieve the "memberof" attribute, filter the array as needed and join it to output to the column.
    7.  Test the code.

    You now have the list you wanted.  You can adjust the format as needed.


    • Proposed as answer by Vector BCO Friday, July 31, 2020 9:28 AM
    Friday, July 31, 2020 5:51 AM
  • When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to you help making their work twice or more.

    Help pls Need to add a new column to the output csv in this AD Group/User Script


    Live long and prosper!


    • Proposed as answer by Vector BCO Saturday, August 1, 2020 11:19 AM
    Friday, July 31, 2020 6:57 AM