1

I am trying to read line by line of a text file into the get-adgroup cmdlet to get a list of which users are a member of the groups from a txt file

Clear-Host;
$groups = "./desktop/groups.txt"
$arrayofgroups = Get-Content $groups 

foreach ($item in $arrayofgroups) {
    Get-ADGroup $item -properties * | select samaccountname, members
}

The text file looks like this:

"group 1"
"group 2"

The below code returns:

Get-ADGroup : 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 again." At line:6 char:15 + {get-adgroup <<<< $item -properties * | select samaccountname, members} + CategoryInfo : WriteError: (:) [Get-ADGroup], ParameterBindingException + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory.Management.Commands.GetADGroup

However, when I loop through the array, it returns to the group name ok

Clear-Host;
$groups = "./desktop/groups.txt"
$arrayofgroups = Get-Content $groups 

foreach ($item in $arrayofgroups ) {
    $item
}

I have verfied that if you simply run the below it works fine.

get-adgroup "group 1" -properties * | select samaccountname, members

I'm new to PowerShell from VBscript, and am not sure why this doesn't work, any thoughts?

1
  • What does $arrayofgroups.count return? If those names are quoted in the file, you should remove the quotes. Commented Jan 15, 2014 at 16:45

1 Answer 1

2

Set a PSBreakpoint on the line containing the Get-ADGroup command, within the foreach loop. In the debugger, type $Item to examine the value of the $Item variable. Also try typing $Item.Length to see how many characters long it is exactly. Make sure that your source text file doesn't have any extra, unnecessary whitespace in it.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent Idea, the variable was '"group 1"' I removed the double quotes from the text file and it processed normally

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.