# Not able to fill empty array with data from function

**URL:** <https://forums.powershell.org/t/not-able-to-fill-empty-array-with-data-from-function/14846>\
**Category:** PowerShell Help\
**Created:** [August 21, 2020, 10:02am UTC](https://forums.powershell.org/t/not-able-to-fill-empty-array-with-data-from-function/14846 "2020-08-21T10:02:59Z")\
**Posts on this page:** 6\
**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, 10:02am UTC](https://forums.powershell.org/t/not-able-to-fill-empty-array-with-data-from-function/14846/1 "2020-08-21T10:02:59Z")

</div>

Hi folks. i’m using Powershell to extract data values from sharepoint on-prem and into an empty array. So far it does not seem to work. Only when i use Write-Host, the output window shows the data i need. But $hashtable does not seem to capture any of the data i’m trying to pull. $hashtable.count is empty. What am i missing? Thank you

[pre]

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/Stuff](https://dev.mycompany.com/sites/Stuff)”  
#Loop through all Sub Sites  
foreach($Web in $Site.AllWebs)  
{

Write-Host $web.Title

foreach($list in $Web.Lists)  
{  
#Filter Doc Libs, Eliminate Hidden ones  
if(($List.BaseType -eq “DocumentLibrary”) -and ($List.Hidden -eq $false) )  
{  
GetFiles($List.RootFolder)  
}  
}  
}

[/pre]

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [August 21, 2020, 10:22am UTC](https://forums.powershell.org/t/not-able-to-fill-empty-array-with-data-from-function/14846/2 "2020-08-21T10:22:24Z")

</div>

Do not have SharePoint to test, but you have the hashtable variable defined outside the function, so you should be getting errors about op\_addition in the function. Try setting a variable to the foreach and you do not need the += operation. Also, if you want output inside the function, use Write-Verbose. Here is something to try:

```
function Get-SPFiles {
    [CmdletBinding()]
    param (
        $Folder
    )
    begin{}
    process {
        $files = foreach($file in $Folder.Files) {
            if($file.Name -like '*.htm*'){
                Write-verbose ('Processing file {0} at url {1}' -f $file.Name, $file.Url)
                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-verbose ('Processing folder {0}' -f $SubFolder.Name)

                Get-SPFiles($Subfolder)
 
            }
        }
    }
    end {
        $files
    }
}
```

---

<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 22, 2020, 11:41am UTC](https://forums.powershell.org/t/not-able-to-fill-empty-array-with-data-from-function/14846/3 "2020-08-22T11:41:02Z")

</div>

Thanks so much Rob, you are a genius! From my original post, i have the bottom code to call the function. When i execute the entire set of code, it looks perfect in ISE output window, but i’m trying to Export to CSV the value of Write-Host “$($web.URL)” along with the $file.name and $file.URL.

The $web.URL, is the first portion of the full Sharepoint website URL. $file.URL is the second portion of the full Sharepoint URL.

Currently i have the Export-CSV cmdlet after Get-SPFiles($List.RootFolder) but it does not include the 1st critical value of $($web.URL), it only exports the $file.name and $file.URL. Hope my explanation is not too convoluted 🙂 Thank you again

&nbsp;

[pre] #Get the Site collection  
$Site= Get-SPSite “[https://dev.mycompany.com/sites/Stuff](https://dev.mycompany.com/sites/Stuff)”  
#Loop through all Sub Sites  
foreach($Web in $Site.AllWebs)  
{  
Write-Host “$($web.URL)” ##### THIS is the VALUE i need to export to CSV as well  
foreach($list in $Web.Lists)  
{  
#Filter Doc Libs, Eliminate Hidden ones  
if(($List.BaseType -eq “DocumentLibrary”) -and ($List.Hidden -eq $false) )  
{  
Get-SPFiles($List.RootFolder) | Export-Csv -Path d:\wo\abc.csv -NoTypeInformation -Append ### Only exports $file.name and $file.URL  
}  
}  
} [/pre]

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [August 22, 2020, 12:00pm UTC](https://forums.powershell.org/t/not-able-to-fill-empty-array-with-data-from-function/14846/4 "2020-08-22T12:00:40Z")

</div>

Try something like this:

```
#Get the Site collection
$Site= Get-SPSite 'https://dev.mycompany.com/sites/Stuff'
#all results returned roll up to the $results variable
$results = foreach($Web in $Site.AllWebs) {
 
    #Get all of the files from the function and put them in the $files variable
    $files = foreach($list in $Web.Lists){
        #Filter Doc Libs, Eliminate Hidden ones
        if(($List.BaseType -eq 'DocumentLibrary') -and ($List.Hidden -eq $false) ) {
            GetFiles($List.RootFolder)
        }
    }

    #Use a calculated expression to append web items to the files.
    $files | Select *,
                    @{Name='Title';Expression={$web.Title}},
                    @{Name='Url';Expression={$web.Url}}
    
}
```

---

<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 22, 2020, 1:21pm UTC](https://forums.powershell.org/t/not-able-to-fill-empty-array-with-data-from-function/14846/5 "2020-08-22T13:21:50Z")

</div>

Thanks Rob! piping out the $results gives me all 3 values we needed. Much appreciated sir.

---

<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/not-able-to-fill-empty-array-with-data-from-function/14846/6 "2024-05-16T20:29:02Z")

</div>

Closed on May 16, 2024, 8:29pm UTC


