Powershell ISE Illegal characters in path with literalpath

Step to IT 80 Reputation points
2024-05-27T08:27:00.97+00:00

Hello!

I have encountered the problem of running PowerShell cmdlets using "LiteralPath" in PowerShell ISE, when folder is existing.

Error message:


Get-ChildItem : Illegal characters in path.
At line:1 char:1
+ Get-ChildItem -LiteralPath "\\?\C:\Test_Folder" -Directory -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.GetChildItemCommand


What is interesting:

  • The error occurs on the Windows server (my version is 2022 with last updates), the error does not occur on the Windows desktop
  • The error occurs in PowerShell ISE, the error does not occur in PowerShell regular

Example of cmdlets:

Test-Path -LiteralPath "\\?\C:\Test_Folder"
Get-ChildItem -LiteralPath "\\?\C:\Test_Folder" -Directory -Recurse

What is the reason for this and how to deal with it?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,430 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,416 Reputation points
    2024-05-27T15:05:33.22+00:00

    Is it possible that the use of long file names hasn't been enabled?

    Check the LongPathsEnabled value in this registry key:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem

    If it isn't set to "1", change it to "1" and reboot the machine.


  2. Rich Matheisen 45,416 Reputation points
    2024-05-28T19:24:47.5433333+00:00

    Try it this way (it really shouldn't make any difference -- I expect it will still fail:

    $MyPath = "\\?\C:\Test_Folder"
    Test-Path -LiteralPath $MyPath
    

    Are you running PowerShell ISE 32-bit or 64-bit? Paste this into the ISE command prompt window and run it: [Environment]::Is64BitProcess. Do you get a True or False as the answer?

    Can you access a FILE (instead of a folder) using the long-file-name format and the -LiteralPath?

    Lastly, is there any reason you're using -LiteralPath and the long-file-name format? There's nothing in name that requires it.