Rows of security groups and Distribution groups

Hi all,

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

$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

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

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:

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

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?

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

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

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

1 Like

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

$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

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

My questions were:

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

1 Like

this is the output

Now you are playing dumb. :thinking: 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.

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

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

1 Like

any other idie from your side?

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

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

$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)

Thank for both of you