Since the GetInvalidFileNameChars () only contains one of the four special chars mentioned, you can just use a single .Replace ('\', '\\') instead of all four .Replace ('\','\\') .Replace ('-','\-').Replace ('^','\^').Replace (']','\]'). Input field: Save the package and execute. … Modern file manager with dual-pane browsing, tabs, columns navigation, editable themes, built-in preview, and much more. A regex processor translates a regular expression in the above syntax into an internal representation that can be executed and matched against a string representing the text being searched in. File Name For Invalid Characters I was using sp_OACreate and sp_OAMethod and didn't pay attention to the filenames -- now I have files and folders I cannot delete because Windows cannot find the file. [<>:"/\|? Double click task to edit properties like below. Take care in asking for clarification, commenting, and answering. ]+$"; \w is equivalent of [0-9a-zA-Z_]. Hello, I'm just getting familiar with regex, so that's my skill level on that. Filenames cannot begin or end with a space, they cannot end with a . A regular expression to match valid filenames. There are actually three things this regex checks for. To ensure the filename is valid, you should use the GetInvalidFileNameChars.NET method to retrieve all invalid character and use a regex to check whether the filename is valid: [regex]$containsInvalidCharacter = '[{0}]' -f ([regex]::Escape([System.IO.Path]::GetInvalidFileNameChars())) if … remove invalid characters from filenames Path.GetInvalidFileNameChars Method (System.IO) | Microsoft Docs