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?