Having trouble running easy Get-ADGroupMember script.

Posted on 2014-08-28
Last Modified: 2014-08-28
Here is my script:

$groups = Get-Content C:\Scripts\Adgroups.txt
foreach($Group in $Groups) {            

Get-ADGroupMember -Id $Group | select  @{Expression={$Group};Label="Group Name"},Name | Export-CSV C:\Scripts\GroupMembersProd.CSV -NoTypeInformation


Open in new window

When I run it I either get:

Get-ADGroupMember : Cannot bind parameter 'Identity' to the target. Exception setting "Identity": "Cannot validate argument on parameter: 'Identity'. The argument is null or empty. Supply an argument that is not null or empty and then try the command

Or my CSV only has the members to the last group in my text file.
Question by:Chad Franks
    LVL 40

    Accepted Solution

    Probably your input file will have empty lines.. Change first line of code to following..
    $groups = Get-Content C:\Scripts\Adgroups.txt | ?{$_ -ne $null -and $_.trim()  -ne ""}

    Open in new window

    Also Export-csv should be outside the foreach loop, updated code follows..
    Get-Content C:\Scripts\Adgroups.txt | ?{$_ -ne $null -and $_.trim() -ne ""} | %{
     $Group = $_ 
     Get-ADGroupMember -Id $Group | 
     select  @{Expression={$Group};Label="Group Name"},Name
    } | Export-CSV C:\Scripts\GroupMembersProd.CSV -NoTypeInformation

    Open in new window

    LVL 6

    Author Closing Comment

    by:Chad Franks
    That worked perfectly.  Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
    Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now