title: How to strip illegal characters before trying to save filenames?
body: <p>I was able to find how to use the <code>GetInvalidFileNameChars()</code> method in a PowerShell script.  However, it seems to also filter out whitespace (which is what I DON'T want).</p>
  
  <p>EDIT:  Maybe I'm not asking this clearly enough.  I want the below function to INCLUDE the spaces that already existing in filenames.  Currently, the script filters out spaces.</p>
  
  <pre><code>Function Remove-InvalidFileNameChars {
  
  param([Parameter(Mandatory=$true,
      Position=0,
      ValueFromPipeline=$true,
      ValueFromPipelineByPropertyName=$true)]
      [String]$Name
  )
  
  return [RegEx]::Replace($Name, "[{0}]" -f ([RegEx]::Escape([String][System.IO.Path]::GetInvalidFileNameChars())), '')}
  </code></pre>