title: Powershell - Suppress errors witihn a function (Out-Null) how to get it working
body: <p>Ive found a useful powershell script on this site with a function for counting files/folder sizes.</p>
  
  <p>Im using this because it is fast and low memory use for large files/folders.</p>
  
  <p>The problems is that when it encounters a folder it doesn't have access to I get output to the console saying access denied.</p>
  
  <pre><code>Exception calling "GetFiles" with "0" argument(s): "Access to the path 'c:\users\administrator\AppData\Local\Applicati\
  n Data' is denied."
  At line:4 char:37
  +         foreach ($f in $dir.GetFiles &lt;&lt;&lt;&lt; ())
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : DotNetMethodException
  </code></pre>
  
  <p>I know, or think I need to use |Out-Null to suppress the errors, but still have the script work, however I just cannot figure out where or how to do this, despite multiple attempts.</p>
  
  <p>So heres the script if anyone has any ideas?</p>
  
  <pre><code>function Get-HugeDirStats ($directory) {
      function go($dir, $stats)
      {
          foreach ($f in $dir.GetFiles())
          {
              $stats.Count++ 
              $stats.Size += $f.Length
          } 
          foreach ($d in $dir.GetDirectories())
          {
              go $d $stats
          } 
      }
      $statistics = New-Object PsObject -Property @{Count = 0; Size = [long]0 }
      go (new-object IO.DirectoryInfo $directory) $statistics
      $statistics
  }
  $stats = Get-HugeDirStats c:\users
  </code></pre>