title: Misbehaving Get-ChildItem Operation in PowerShell
body: <p>There is a back-end SQL DB contains "managed folders" in the form of UNC paths. Using SQL queries in PowerShell I have a loop that will work it's way through these folders and run a GCI operation against them to work out how much disk space they are using. </p>
  
  <pre><code>$managedFolder = "\\server\share\folder\subfolder"
  </code></pre>
  
  <p>For the sake of the question, <code>$managedFolder</code> is declared as above. The failing command below: </p>
  
  <pre><code>$diskTrendsInitialUsage = "{0:N2}" -f ((Get-ChildItem $managedFolder -Recurse -Force | Measure-Object -Property Length -Sum).Sum / 1GB)
  </code></pre>
  
  <p>Now if I were to run this command manually in PS console it's fine, it pulls back data. But as soon as it's packaged in a script, it fails with the below error. The folder is accessible from the server, as it works fine from a local PS console session. </p>
  
  <pre><code>ERROR: Get-ChildItem : Invalid Path: '\\server\share\folder\subfolder'.
  AddManagedFolder.psf (17): ERROR: At Line: 17 char: 42
  ERROR: +         $diskTrendsInitialUsage = "{0:N2}" -f ((Get-ChildItem $managedFolder -Recurse  ...
  ERROR: +                                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ERROR:     + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], ArgumentException
  ERROR:     + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.GetChildItemCommand
  ERROR:
  </code></pre>
  
  <p>I'm stumped. </p>