Powershell: Add object to an array in a function

Dieter Devroey 21 Reputation points
2020-12-22T13:15:33.647+00:00

I need to make a script that writes user id's with certain attributes to a csv file. Before I lookup all the attributes I want to collect the user id's from different sources into an array. When I add the first users to the array, the array is holding these user id's. However, when I want to add user id's for the second time, the array is empty again.

$array3 = @()
$array = @()

function get-start {
    Write-Host "Function Get-Start started"
    $text = Get-Content -Path C:\Data\text.txt
    foreach ($Line in $text)
    {
        If ($Line -eq "text1.txt")
        {
            Write-Host "$Line is found"
            Get-Text1
        }
        Elseif ($Line -eq "text2.txt")
        {
            Write-Host "$Line is found"
            Get-Text2
        }
    }
}

function Get-Text1 {
    Write-Host "function Get-Text1 started"
    $array = Get-Content -Path C:\Data\text1.txt
    Write-Host "content of text1.txt is $array"
    Add-Arrays
}

function Get-Text2 {
    Write-Host "function Get-Text2 started"
    $array = Get-Content -Path C:\Data\text2.txt
    Write-Host "content of text2.txt is $array"
    Add-Arrays
}

function Add-Arrays {
    Write-Host "function Add-Arrays started"
    Write-Host "content of array3 is $array3"
    $array3 += $array
    Write-Host "content of array3 now is $array3"
}

get-start

the outcome of this code is the following

PS D:\DMS> C:\Data\Test_Arrays.ps1
Function Get-Start started
text1.txt is found
function Get-Text1 started
content of text1.txt is Rood Groen Blauw Oranje Paars Zwart Wit
function Add-Arrays started
content of array3 is 
content of array3 now is Rood Groen Blauw Oranje Paars Zwart Wit
text2.txt is found
function Get-Text2 started
content of text2.txt is Appel Peer Banaan Kiwi Appelsien Mandarijn Perzik
function Add-Arrays started
content of array3 is 
content of array3 now is Appel Peer Banaan Kiwi Appelsien Mandarijn Perzik

As you can see the content of $array3 is empty before I add content to it and is empty again when I want to add content for a second time.
How can I let powershell remember the content?

Thank you in advance

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2020-12-22T14:27:06+00:00

    Could you please try this as the Add-Arrays function in your script please:

    function Add-Arrays {
         Write-Host "function Add-Arrays started"
         Write-Host "content of array3 is $array3"
         $global:array3 += $array
         Write-Host "content of array3 now is $array3"
     }
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


5 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2020-12-22T13:40:26.357+00:00

    To combine 2 arrays maybe this helps:

    $array1 = 2,3,4,5
    $array2 = 1,6,7,8,9
    $arrayAll = $array1 + $array2
    $arrayAll
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Dieter Devroey 21 Reputation points
    2020-12-22T13:48:25.217+00:00

    Using the + approach has the same result.
    The function Add-Arrays works but it does not remember what the content of array3 is.

    As you can see in the output of the script, the content of array3 is always empty when the function is called.
    Eventually array3 must hold all the fruits and colors.

    0 comments No comments

  3. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2020-12-22T14:27:20.193+00:00

    Could you please try this as the Add-Arrays function in your script please:

    function Add-Arrays {
         Write-Host "function Add-Arrays started"
         Write-Host "content of array3 is $array3"
         $global:array3 += $array
         Write-Host "content of array3 now is $array3"
     }
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  4. Rich Matheisen 47,901 Reputation points
    2020-12-22T15:49:47.897+00:00

    Why are you using functions to accomplish this? I'm not at all opposed to using functions when they're appropriate and can be used to create reusable code, or simplify a script by isolating/refactoring code, but the use of three additional functions seems to just complicate things in this case.

    See if this works for you.

    $array = @()
    $filenames = "text1.txt", "text2.txt"
    function get-start {
        Write-Host "Function Get-Start started"
        Get-Content -Path C:\Data\text.txt |
            ForEach-Object {
                If ($filenames -contains $_){
                    Write-Host "$_ is found"
                    $script:array += Get-Content -Path "C:\Data\$_"
                }
            }
    }
    
    get-start
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.