1

I have got Strings in an array which are the names of groups. Now I would like to modify those values and connect an other String to the beginning of this String

$Groups = Get-ADPrincipalGroupMembership $User $GroupArray = @()

foreach ($Group in $Groups)
{
$GroupArray += ($Group | select name)
} 
echo $("Domain\" + $GroupArray[0])

This prints something like:

Domain\@{name=Domain Users}

However I would like to get something like:

Domain\Domain Users
4
  • "Domain\$(GroupArray[0].Name)" Commented Apr 24, 2019 at 10:58
  • 1
    since you used Select-Object Name you have an object with a property named Name. it seems that you want to have JUST the value from the prop. if that is the case, then change that to Select-Object -ExpandProperty Name to get just the value. Commented Apr 24, 2019 at 11:00
  • Why not arraylist? Then Arraylist.add(value).. It seems a lot easier to deal with. Commented Apr 24, 2019 at 11:01
  • 1
    The easiest way is to just set a variable equal to the foreach loop and output the data you want within the loop. Commented Apr 24, 2019 at 11:03

1 Answer 1

1

Change it to arraylist if you wish to add all the values:

$Groups = Get-ADPrincipalGroupMembership $User

$arraylist = New-Object System.Collections.ArrayList
foreach ($Group in $Groups)
{
$arraylist.Add($Group.Name) | Out-Null
} 

$arraylist

PS: You can display the result collating with the Domain however you want; I have not touched that section. Hope it helps.

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

1 Comment

@Andr0mega: Happy to help. Enjoy coding :)

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.