title: 'Get-ChildItem -Include/-Exclude' in PowerShell doesn't filter if passed as an argument to a function
body: <p>I have a problem getting a filter argument to <a href="http://technet.microsoft.com/en-us/library/hh849800.aspx" rel="nofollow noreferrer">Get-ChildItem</a> in a function.</p>
  
  <p>The following works fine and displays a whole list of files:</p>
  
  <pre><code>c:\temp\Get-ChildItem -Include *deleteme*.txt -Recurse
  </code></pre>
  
  <p>Now say I have the following script</p>
  
  <pre><code>#file starts here
  #filename = GetLastFile.ps1
  
  param([string] $filter)
  
  $files = Get-ChildItem $filter
  
  Write-Host $files #should print all matching files but prints nothing
  
  $file = $files | Select-Object -Last 1;
  
  
  $file.name  #returns filename
  #File ends here
  </code></pre>
  
  <p>Now trying to run the script,</p>
  
  <pre><code>c:\temp.\GetLastFile.ps1 "-Include *deleteme*.txt -Recurse"
  </code></pre>
  
  <p>returns nothing.</p>
  
  <p>Supplying a filter, <code>*.*</code>, works fine. It seems to be failing due to the <code>-Include</code> or <code>-Exclude</code>. Any ideas?</p>