# Adding values from function into new PSobject array

**URL:** <https://forums.powershell.org/t/adding-values-from-function-into-new-psobject-array/14845>\
**Category:** PowerShell Help\
**Created:** [August 21, 2020, 9:50am UTC](https://forums.powershell.org/t/adding-values-from-function-into-new-psobject-array/14845 "2020-08-21T09:50:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![powershellfan](https://avatars.discourse-cdn.com/v4/letter/p/839c29/32.png) [@powershellfan](https://forums.powershell.org/u/powershellfan)\
**Post date:** [August 21, 2020, 9:50am UTC](https://forums.powershell.org/t/adding-values-from-function-into-new-psobject-array/14845/1 "2020-08-21T09:50:51Z")

</div>

Hi folks. i’m working on pulling some data from Sharepoint on-prem, using Powershell and currently it outputs perfectly when i use “Write-Host”. But i need to add the values to an open array and order it so i can Export to CSV file. Can you tell me why the $Hashtable array is empty and does not store any of the filename and fileURL values? Write-Host shows them, but $hashtable is empty. Thank you

```
#Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$Hashtable = @()

Function GetFiles($Folder) {

    foreach ($file in $Folder.Files) {
        if ($file.Name -like "*.htm*") {

            Write-Host $file.Name
            Write-Host $file.URL
            $Hashtable += New-Object PSObject -Property @{
                ‘filename’ = $file.Name;
                ‘fileURL’ = $file.URL;
            }
        }
    }
    #Loop through all subfolders and call the function recursively
    foreach ($SubFolder in $Folder.SubFolders) {
        if ($SubFolder.Name -ne "Forms") {
            Write-Host "`t" -NoNewline
            GetFiles($Subfolder)

        }
    }
}

#Get the Site collection
$Site = Get-SPSite "https://dev.MyCompany.com/sites/Problems”
#Loop through all Sub Sites
foreach($Web in $Site.AllWebs){
Write-Host "—————————————————–"
Write-Host "Site Name: '$($web.Title)' at $($web.URL)"
Write-Host "—————————————————–"
    foreach($list in $Web.Lists){
    #Filter Doc Libs, Eliminate Hidden ones
        if(($List.BaseType -eq "DocumentLibrary") -and ($List.Hidden -eq $false) ){
            GetFiles($List.RootFolder)
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![adminofthings](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@adminofthings](https://forums.powershell.org/u/adminofthings)\
**Post date:** [August 21, 2020, 10:20am UTC](https://forums.powershell.org/t/adding-values-from-function-into-new-psobject-array/14845/2 "2020-08-21T10:20:17Z")

</div>

The reason `$hashTable` contains no data after your function call is because of how scoping works. When a function is called, it runs in its own scope. Variables updated within a function are not reflected outside of the function. There are exceptions of course if you use scope modifiers. To achieve the results you want, you can just output custom objects from within your `foreach` loop. Then save your function output into a list, which will contain an `Add()` method for adding elements. In order for your output to be a single dimensional array, you will need to iterate your `GetFiles()` output with your current function design.

```
$OutputArray = [collections.generic.list[psobject]]@() 
Function GetFiles($Folder) { 
 
    foreach ($file in $Folder.Files) { 
        if ($file.Name -like "*.htm*") { 
            Write-Host $file.Name 
            Write-Host $file.URL 
            [pscustomobject]@{ 'filename' = $file.Name; 'fileURL' = $file.URL } 
        } 
    } 
 
    #Loop through all subfolders and call the function recursively 
    foreach ($SubFolder in $Folder.SubFolders) { 
        if ($SubFolder.Name -ne "Forms") { 
            Write-Host "`t" -NoNewline 
            GetFiles($Subfolder) 
 
        } 
    } 
} 
 
foreach ($List in $Web.Lists) { 
 
    GetFiles($List.RootFolder) | Foreach-Object { $OutputArray.Add($_) } 
}
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/adding-values-from-function-into-new-psobject-array/14845/3 "2024-05-16T20:29:02Z")

</div>


