Forum Discussion

Schulzi's avatar
Schulzi
Brass Contributor
Feb 25, 2021
Solved

foreach loop, self creating variables

I have following (part) of my script:     $form_Users = New-Object System.Windows.Forms.Form $form_Users.Text = "Users to add" $form_Users.Size = New-Object System.Drawing.Size (300,200) $form_Us...
  • CoasterKaty's avatar
    Feb 25, 2021

    Schulzi 

     

    You'd just need to use an array and populate that within the foreach loop:

    $selected_Users = $selected_Output -split ","
    $AllMail = @()
    foreach($User in $selected_Users)
    {
        $selected_email = "$User@mail.com"
        $AllMail += ,$selected_email
    }
    $AllMail

     which would leave you with $AllMail as an array containing all the e-mail addresses.

    If you wanted a single variable rather than an array, with a comma separated list, you'd do this at the end:

    $EmailList = $AllMail -join ","

Resources