title: PowerShell Scripting - Get-ChildItem
body: <p>I've written a script that will be used for archiving log files from a server. I'm in pretty good shape with everything but the recursiveness or not of <a href="http://technet.microsoft.com/en-us/library/hh849800.aspx" rel="nofollow noreferrer">Get-ChildItem</a>...</p>
  
  <p>The issue I seem to be having is that when Get-ChildItem is not recursive and <code>-Include</code> is present with only one filter, it is ignored! Or, I'm doing something wrong (likely).</p>
  
  <p>I've cleaned up the output a little...</p>
  
  <pre><code>PS C:\foo&gt; Get-childitem -path "c:\foo"
  
  Name
  ----
  bar1.doc
  bar2.doc
  bar3.doc
  foo1.txt
  foo2.txt
  foo3.txt
  
  PS C:\foo&gt; Get-childitem -path "c:\foo" -Include *.txt
  PS C:\foo&gt; Get-childitem -path "c:\foo" -Include *.txt -recurse
  
  Name
  ----
  foo1.txt
  foo2.txt
  foo3.txt
  </code></pre>
  
  <p>Sooo??? I had a fantasy where all I had to do was branch to a path of the script that did not have the recurse switch. (By the way, is it possible to variably apply parameters so as to avoid duplicated code paths where the only variability is the parameters to a cmdlet?)</p>
  
  <p>Anyway, here is my script for completeness, in addition to my issue with Get-ChildItem.</p>
  
  <pre><code>function MoveFiles()
  {
      Get-ChildItem -Path $source -Recurse -Include $ext | where { $_.LastWriteTime -lt (Get-Date).AddDays(-$days) } | foreach {
          $SourceDirectory = $_.DirectoryName;
          $SourceFile = $_.FullName;
          $DestinationDirectory = $SourceDirectory -replace [regex]::Escape($source), $dest;
          $DestionationFile = $SourceFile -replace [regex]::Escape($source), $dest;
  
          if ($WhatIf){
              #Write-Host $SourceDirectory;
              #Write-Host $DestinationDirectory;
              Write-Host $SourceFile -NoNewline
              Write-Host " moved to " -NoNewline
              Write-Host $DestionationFile;
          }
          else{
              if ($DestinationDirectory)
              {
                  if ( -not [System.IO.Directory]::Exists($DestinationDirectory)) {
                      [void](New-Item $DestinationDirectory -ItemType directory -Force);
                  }
                  Move-Item -Path $SourceFile -Destination $DestionationFile -Force;
              }
          }
      }
  }
  </code></pre>