Powershell illegal characters in path

Powershell – Illegal Characters in Path

When working with file paths in PowerShell, there are certain characters that are considered illegal and cannot be used in a path. These characters can cause issues and errors when attempting to create, modify, or delete files or directories.

The following characters are considered illegal in file paths:

  • \ (backslash)
  • / (forward slash)
  • : (colon)
  • * (asterisk)
  • ? (question mark)
  • (double quote)
  • < (less than)
  • > (greater than)
  • | (pipe)

Using any of these characters in a file or directory name can lead to errors like “illegal characters in path” or “invalid path”.

Examples:

Let’s consider a few examples:

  • Example 1:
  • $path = "C:\Directory\file*.txt"
    New-Item -Path $path -ItemType File

    In this example, the asterisk (*) character is used in the file name. This will result in an “illegal characters in path” error, as the asterisk is one of the illegal characters.

  • Example 2:
  • $path = "C:\Directory\file".txt"
    New-Item -Path $path -ItemType File

    Here, a double quote (“) is used in the file name. Again, this will throw an error as the double quote is an illegal character.

  • Example 3:
  • $path = "C:\Directory\file<.txt"
    New-Item -Path $path -ItemType File

    In this example, the less than (<) character is used in the file name. This will also result in an error due to the usage of an illegal character.

To avoid such errors, make sure to exclude the illegal characters from your file and directory names when working with PowerShell.

Leave a comment