[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Having trouble running easy Get-ADGroupMember script.

Posted on 2014-08-28
Medium Priority
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

Subsun earned 2000 total points
ID: 40290652
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


Author Closing Comment

by:Chad Franks
ID: 40290900
That worked perfectly.  Thanks!

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

834 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