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

Answer accepted by question author
  1. Andreas Baumgarten 129.5K 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. Anonymous
    2020-12-23T10:00:50.403+00:00

    Hi,

    You actually created a new child scope every time you called function Add-Arrays and a new variable named array3 was created in the scope. Adding using makes you access variables defined in other scopes.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.1

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

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