title: Deleting files with special characters
body: <p>I want to purge log files from a build server, keeping the last few days. I have no trouble with the log files my process creates. This works fine:</p>
  
  <pre><code>$logFolders = Get-ChildItem $buildBase -Recurse -include logs -Attributes D
  $logFolders | ForEach-Object { Get-ChildItem $_.fullname -filter *.log | where &gt;{$_.lastwritetime -lt (get-date).adddays(-$purgeAfter) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force} };
  </code></pre>
  
  <p>However, the logs produced by the build tool look like this:</p>
  
  <pre><code>lrFR_WebHelp - Friday, February 28, 2014 [9.00 PM].mclog
  </code></pre>
  
  <h3>I've tried quoting the file names:</h3>
  
  <pre><code>Get-ChildItem $frReports\Reports -filter *.mclog | where {$_.lastwritetime -lt (get-date).adddays(-$purgeAfter) -and -not $_.psiscontainer}| % {write-host ("'" + $frReports + '\Reports'  + '\' + $_ + "'") }
  </code></pre>
  
  <p>Outputs:</p>
  
  <pre><code>'D:\scratch\Reports\lrFR_WebHelp - Friday, February 28, 2014 [9.00 PM].mclog'
  </code></pre>
  
  <p>But attempting to delete:</p>
  
  <pre><code>Get-ChildItem $frReports\Reports -filter *.mclog | where {$_.lastwritetime -lt (get-date).adddays(-$purgeAfter) -and -not $_.psiscontainer}| % {remove-item ("'" + $frReports + '\Reports'  + '\' + $_ + "'") }
  </code></pre>
  
  <p>Outputs:</p>
  
  <pre><code>remove-item : Cannot find drive. A drive with the name ''D' does not exist.
  At line:1 char:145
  + ... container}| % {remove-item ("'" + $frReports + '\Reports'  + '\' + $_ + "'") }
  +                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : ObjectNotFound: ('D:String) [Remove-Item], DriveNotFoundException
      + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
  </code></pre>
  
  <p>Changing to unix slashes ('/') or escaped slashes ('\\') doesn't change the result</p>
  
  <h3>I tried working around the path. Got no errors, but also didn't delete any files.</h3>
  
  <p>This:</p>
  
  <pre><code>    $filesToDelete = Get-ChildItem $frReports\Reports -filter *.mclog | where {$_.lastwritetime -lt (get-date).adddays(-$purgeAfter) -and -not $_.psiscontainer}
  
      Push-Location -path D:\scratch\Reports
  
      $filesToDelete | ForEach-Object { 
          $fname = ("'" +  $_ + "'");
          write-host $fname;
          remove-item $fname -Force;
          remove-item $_;
  }
  </code></pre>
  
  <p>Outputs this:</p>
  
  <pre><code>'lrFR_WebHelp - Friday, February 28, 2014 [9.00 PM].mclog'
  'lrFR_WebHelp - Friday, March 14, 2014 [10.24 PM].mclog'
  </code></pre>
  
  <p>But doesn't delete the files.</p>
  
  <h3>Finally, after snooping around the internet for clues, I tried the fso object</h3>
  
  <pre><code>   $fso = New-Object -ComObject Scripting.FileSystemObject
      $buildBase = "D:\scratch";
      $logFolders = Get-ChildItem $buildBase -Recurse -include Reports -Attributes D
  
  
      $logFolders | ForEach-Object { Get-ChildItem $fso.GetFile($_.FullName).ShortPath -filter *.MCL | where {$_.lastwritetime -lt (get-date).adddays(-$purgeAfter) -and -not $_.psiscontainer}} #|% {write-host $_.fullname}  };
  </code></pre>
  
  <p>Outputs:</p>
  
  <pre><code>Exception calling "GetFile" with "1" argument(s): "Exception from HRESULT: 0x800A0035 (CTL_E_FILENOTFOUND)"
  At line:1 char:32
  + $logFolders | ForEach-Object { Get-ChildItem $fso.GetFile($_.FullName).ShortPath ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : ComMethodTargetInvocation
  </code></pre>
  
  <p>Didn't have any luck getting more information about $fso.GetFile in PowerShell. Google failed me this time.</p>
  
  <p>Can anyone help? I'd prefer a pure PowerShell solution, but will be grateful for anything that works.</p>