How to add values to separate lines in an array?

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of I Am Sir Ask Alot I Am Sir Ask Alot 1 year, 11 months ago.

  • Author
    Posts
  • #21767
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    I am trying to figure out how to create new lines where each testuser is contained on its own line element.

    When I run the following code, the testusers all get bunched up together under the Value property heading.

    $AdUser = @("TestUser1", "TestUser2", "TestUser3")

    $Array = @{}

    Foreach ($User in $ADUser)
    {
    $UPN = $User + "@myemail.com"

    $Array['EmailAddress'] += $upn
    }

    $Array

    Can someone help me out with this, please?

    Thanks a bunch, I appreciate it.

  • #21768
    Profile photo of Stein Petersen
    Stein Petersen
    Participant

    Hi.

    The $Array is declared as a hashtable so you would add entries to that one,

    $AdUser = @("TestUser1", "TestUser2", "TestUser3")
    
    $Array = @{}
    
    Foreach ($User in $ADUser) {
    
       $UPN = $User + "@myemail.com"
       $Array.add($user,$UPN)
    
    }
    
    $Array
    
    

    Stein

    [i]Dave Wyatt: Edited as per OP's request.[/i]

  • #21769
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    Stein that worked!

    I also was playing around and I came up with this too that works as well:

    $ADUser = @("TestADUser1", "TestADUser2" , "TestADUser3")

    $Array = @()
    Foreach ($User in $ADUser)
    {
    $Email = $User + "@myemail.com"

    $OutputObj = New-Object -TypeName PSobject
    $OutputObj | Add-Member -MemberType NoteProperty -Name Email -Value $Email

    $Array += $OutputObj
    }

    $Array

    Thanks for your help

You must be logged in to reply to this topic.