# Rows of security groups and Distribution groups

**URL:** https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049
**Category:** PowerShell Help
**Created:** [April 8, 2022, 8:35pm UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049 "2022-04-08T20:35:05Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![alexxx55555](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@alexxx55555](https://forums.powershell.org/u/alexxx55555)
#### Post date: [April 8, 2022, 8:35pm UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/1 "2022-04-08T20:35:05Z")

</div>

Hi all,

I’m using this script to copy groups from one use to another:

```auto
$SourceUser = $null

$Message = "Copy Groups From"
While (!$SourceUser) {
    $copyfrom = Read-Host -Prompt $Message
    Try {
        $SourceUser = Get-ADUser -Identity $copyfrom -Properties memberof -ErrorAction Stop
    } Catch {
        
       [void][System.Windows.Forms.MessageBox]::Show(($Message = "User not found. Please enter a user to copy groups from"))
    }
}
$SelectedGroups = $SourceUser.memberof | Add-ADGroupMember -Members $username -PassThru | Select-Object -ExpandProperty Name
$Grouptype = $SourceUser.memberof | Add-ADGroupMember -Members $username -PassThru | Select-Object -ExpandProperty GroupCategory

```

The result that I’m getting is like this  
 ![2022-04-08_23-33-52](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/c/c7cb2e33758dc768813071d1c268358c0da4dad0.jpeg)

but i want to make 2 rows one with group that copied DL  
and one with groups that copied security groups

any idea how can I do it?

Thank you

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [April 8, 2022, 10:37pm UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/2 "2022-04-08T22:37:57Z")

</div>

Assumed you defined the variable `$username` before you run the code you shared here this should be enough to transfer the groups and show what you want after your while loop ran:

```auto
$Result = $SourceUser.memberof | Add-ADGroupMember -Members $username -PassThru 
$Result | Select-Object -Property Name,GroupCategory

```

---

<div class="post-metadata">

### Author: ![alexxx55555](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@alexxx55555](https://forums.powershell.org/u/alexxx55555)
#### Post date: [April 9, 2022, 6:30am UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/3 "2022-04-09T06:30:29Z")

</div>

Hi Olaf,  
Hope you doing well what I want to show is something like this

DL groups:All-company, all-it  
Security groups: admin,domain admin

can it be done?

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [April 9, 2022, 8:22am UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/4 "2022-04-09T08:22:42Z")

</div>

Did you try my code suggestion? Did it work? What was the output?

---

<div class="post-metadata">

### Author: ![alexxx55555](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@alexxx55555](https://forums.powershell.org/u/alexxx55555)
#### Post date: [April 9, 2022, 9:35am UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/5 "2022-04-09T09:35:55Z")

</div>

I tried what i add to the script  
I showed the result in the pic

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [April 9, 2022, 10:11am UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/6 "2022-04-09T10:11:45Z")

</div>

OK, so why don’t you try my code suggestion?

---

<div class="post-metadata">

### Author: ![alexxx55555](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@alexxx55555](https://forums.powershell.org/u/alexxx55555)
#### Post date: [April 9, 2022, 3:19pm UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/7 "2022-04-09T15:19:46Z")

</div>

> [@Olaf](#):
>
> ```auto
> $Result = $SourceUser.memberof | Add-ADGroupMember -Members $username -PassThru 
> $Result | Select-Object -Property Name,GroupCategory
> 
> ```

i tried your suggestion but i don’t see this result  
im using this code to get the resoult

```auto
$subject = "New Users Created"
    $Message =
    "User Created: 
    First Name: $firstname 
    Last Name: $lastname 
    Employess number: $newNum 
    Username: $username 
    E-mail: $email 
    Sip: $SFB
    Groups: $($SelectedGroups -join ",")
    Initial Password: $password

```

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [April 9, 2022, 4:14pm UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/8 "2022-04-09T16:14:24Z")

</div>

> [@alexxx55555](#):
>
> i tried your suggestion but i don’t see this result

Sorry if that sounds rude but I’d recommend to start reading and answering questions carefully.

My questions were:

> [@Olaf](#):
>
> Did you try my code suggestion? Did it work? **What was the output?**

The code you posted last has neither to do with any of your code you posted earlier nor mine.

---

<div class="post-metadata">

### Author: ![alexxx55555](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@alexxx55555](https://forums.powershell.org/u/alexxx55555)
#### Post date: [April 9, 2022, 6:02pm UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/9 "2022-04-09T18:02:34Z")

</div>

> [@Olaf](#):
>
> ```auto
> $Result = $SourceUser.memberof | Add-ADGroupMember -Members $username -PassThru 
> $Result | Select-Object -Property Name,GroupCategory
> 
> ```

this is the output

 ![b](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/f/faf12a0b9ed14dfd4f02223e3e9614205db408ca.jpeg)

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [April 9, 2022, 6:09pm UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/10 "2022-04-09T18:09:05Z")

</div>

> [@alexxx55555](#):
>
> this is the output

Now you are playing dumb. 🤔 Of course I was talking about the output my code suggestion produces.

Regardless of that - the output you produce with a graphical popup or with `Write-Host` for example is absolutely useless for everything else than the persons eyes running the code. A more professional attempt would be to output it as CSV or any other reusable data structure.

---

<div class="post-metadata">

### Author: ![alexxx55555](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@alexxx55555](https://forums.powershell.org/u/alexxx55555)
#### Post date: [April 9, 2022, 6:16pm UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/11 "2022-04-09T18:16:47Z")

</div>

so your code is not doing what I wanted i will find another solution  
I want this pop up for me

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [April 9, 2022, 6:32pm UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/12 "2022-04-09T18:32:28Z")

</div>

> [@alexxx55555](#):
>
> so your code is not doing what I wanted i will find another solution

I did not claim that this is the final solution. We try to provide help to help yourself.

---

<div class="post-metadata">

### Author: ![alexxx55555](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@alexxx55555](https://forums.powershell.org/u/alexxx55555)
#### Post date: [April 9, 2022, 6:43pm UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/13 "2022-04-09T18:43:38Z")

</div>

any other idie from your side?

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [April 9, 2022, 6:49pm UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/14 "2022-04-09T18:49:12Z")

</div>

I will ask this only once more. What’s the output?

---

<div class="post-metadata">

### Author: ![mawazo](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@mawazo](https://forums.powershell.org/u/mawazo)
#### Post date: [April 9, 2022, 8:41pm UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/15 "2022-04-09T20:41:39Z")

</div>

assuming you want dl groups and security groups in the message box. Use olaf’s solution to split the groupcategory maybe with where object?

```auto
$Result = $SourceUser.memberof | Add-ADGroupMember -Members $username -PassThru 

$Message =
@" 
User Created: 
    First Name: $firstname 
    Last Name: $lastname 
    Employess number: $newNum
    Username: $username 
    E-mail: $email
    Sip: $SFB
    DL Groups: $($Result.Where({$_.groupcategory -eq 'distribution'}).name -join ',')
    Security Groups: $($Result.Where({$_.groupcategory -eq 'security'}).name-join ',') 
    Initial Password: $password

Makesure to save the intial password in a safe location!
"@

[System.Windows.forms.messagebox]::Show($Message)

```

---

<div class="post-metadata">

### Author: ![alexxx55555](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@alexxx55555](https://forums.powershell.org/u/alexxx55555)
#### Post date: [April 10, 2022, 4:23am UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/16 "2022-04-10T04:23:25Z")

</div>

> [@mawazo](#):
>
> ```auto
> DL Groups: $($Result.Where({$_.groupcategory -eq 'distribution'}).name -join ',')
> Security Groups: $($Result.Where({$_.groupcategory -eq 'security'}).name-join ',') 
> 
> ```

Thank for both of you

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:18pm UTC](https://forums.powershell.org/t/rows-of-security-groups-and-distribution-groups/19049/17 "2024-05-16T20:18:04Z")

</div>


