Function Remove-InvalidCharacters { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [string] $String, [Parameter(Mandatory=$false)] $ReplacementCharacter = '' ) $InvalidCharacters = [RegEx]::Escape(-join $([System.IO.Path]::GetInvalidFileNameChars())) return [RegEx]::Replace($String, "[$InvalidCharacters]", $ReplacementCharacter) }