Finding Invalid File and Path Characters

by Apr 20, 2016

When dealing with files and folders, some characters are considered illegal. Here is an easy way to get a list of the characters that Windows declared as illegal:

$invalidPath = -join [System.IO.Path]::GetInvalidPathChars()

$invalidFile = -join [System.IO.Path]::GetInvalidFileNameChars()

In a path, a pipeline symbol is considered illegal, and that is exactly what you get when you try and use a pipeline symbol inside a path:

$path = "c:\test\invalid|file\path.txt"
Test-Path $path
Test-Path : Illegal characters in path.
At line:2 char:1
+ Test-Path $path
+ ~~~~~~~~~~~~~~~

Twitter This Tip! ReTweet this Tip!