mao
7/6/2017 - 9:19 AM

Test if $fileName is valid file name. Answers question "How do I check if $fileName is completely valid file name in Windows 10?"

Test if $fileName is valid file name. Answers question "How do I check if $fileName is completely valid file name in Windows 10?"

Function Test-FilenameValid {

  PARAM(  [PARAMETER( Mandatory )]
          [String]
          $filename  
  )

  # IndexOfAny returns -1 if there is no any characters from argument in the string
  $filename.IndexOfAny( [System.IO.Path]::GetInvalidFileNameChars() ) -eq -1

}