Adding values from function into new PSobject array

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)
        }
    }
}

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($_) } 
}