split array

Hi,
I use this
[pre]
$user= “username”
$groups =Get-ADPrincipalGroupMembership $user
$group = $groups.name

Send-MailMessage -To “username@mydomain.com” -From “myuser@mydomain.com” -Subject “List of groups $user is member off” -Body “ $group Best regards” -Credential $emailcred -SmtpServer “outbound.mydomain.com” -Port 25

[/pre]
this will give me the clean name without the “CN=” ect

now I want to send this by mail to a common mailbox but I do get it printed out like groupA groupB groupC

by preference I would like to print this

groupA

groupB

groupC

how do I achieve this?

There is an eBook in Free Resources about HTML reporting you should take a look at, but it’s simpler to use HTML for simple formatting in message bodies:

$user= “username”
$groups = Get-ADPrincipalGroupMembership $user |
          Select @{Name='Group Memberships';Expression={$_.Name}}

$htmlBody = $groups | 
            ConvertTo-Html -PostContent "<br/Regards<br/><br/>IT Folks" -Property 'Group Memberships'

$params = @{
    To         = “username@mydomain.com” 
    From       = “myuser@mydomain.com” 
    Subject    = “Group Membership(s): $user” 
    Body       = $htmlBody
    Credential = $emailcred 
    SmtpServer = “outbound.mydomain.com”
    BodyAsHtml = $true
    Port       = 25
}

Send-MailMessage @params

Dang, Rob is fast! Here’s another way you can accomplish your goal. We both cleaned up the code with splatting, it’s highly recommended for many reasons.

$user = “username”
$groups = Get-ADPrincipalGroupMembership

$msgparams = @{
    To         =  “username@mydomain.com”
    From       =  “myuser@mydomain.com”
    Subject    =  “List of groups $user is member off”
    Body       =  $groups + "Best regards” -join "nn"  # Forum messing this up, supposed to be the grave accent (shift ~) in front of each n
    Credential =  $emailcred
    SmtpServer =  “outbound.mydomain.com”
    Port       =  25
}

Send-MailMessage @msgparams

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-7

thanks for your answers

 

Paul