Home > Net >  Move-Item: Illegal characters in path - filename with spaces
Move-Item: Illegal characters in path - filename with spaces

Time:09-03

I have an issue with moving a file that contains spaces.

This is the example path: $transfer.FileName = D:\this\is\a\file with a lot of spaces.xml

$file = $transfer.FileName
Move-Item -path $file -Destination $archiveFolder

Error:

Move-Item : Illegal characters in path.
At line:1 char:17
                  Move-Item -path $file -Destination $archiveFolder
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidArgument: (D:\this\i...ong 2022-08.xml:FileInfo) [Move-Item], ArgumentException
      FullyQualifiedErrorId : MoveFileInfoItemArgumentError,Microsoft.PowerShell.Commands.MoveItemCommand


When i run test-path $file I receive $true. I have tried replacing the spaces with "` " but after that i got a new error saying that the path was not correct.

Does someone know how I can fix this?

CodePudding user response:

Your problem isn't with the input file, $file, but with the destination path, $archiveFolder:

The error message implies that $archiveFolder contains characters that are illegal in a file-system path, namely (on Windows) * ? < > | and, except as part of a drive spec such as C:, :.

Note that Move-Item's -Destination parameter does not support wildcard expressions.

Therefore:

  • If the value is meant to be a literal path, remove the illegal character and try again.

  • If the value is a wildcard expression, resolve it to a literal path first, and ensure that it resolves to just one literal path:

    # Resolve the presumed wildcard pattern in $archiveFolder
    # to a full, literal path.
    $archiveFolderLiteral = Resolve-Path $archiveFolder
    if ($archiveFolderLiteral.Count -ne 1) { 
      throw "Wildcard expression `"$archiveFolder`" did not resolve to a single, literal path."
    }
    
    # Also note the use of -LiteralPath instead of -Path - see below.
    Move-Item -LiteralPath $file -Destination $archiveFolderLiteral
    

As for the input path:

As long as you quote a path that contains spaces (or other metacharacters) (e.g.
'D:\this\is\a\file with a lot of spaces.xml'),[1] it is correctly passed as a single argument to the target command.

However, there's an additional consideration with respect to file-processing cmdlets:

  • Their -Path parameter (which is also the default parameter if you pass a path positionally as the first argument) expects wildcard expressions, which means that a path that contains [ and ] that is meant to be a literal path gets misinterpreted.

  • Thus, for full robustness, it is best to use the -LiteralPath parameter explicitly whenever your paths are literal ones rather than wildcards.


[1] When passing paths directly as arguments (as opposed to storing them in a variable with $path = ... first), you may alternatively escape individual metacharacters with `, though with multiple spaces, for instance, that becomes cumbersome; e.g., to pass verbatim test file 1: Write-Output test` file` 1

CodePudding user response:

I was not able to replicate your issue.

$filename = "D:\Desktop\New folder\New Text Document.txt"
$archiveFolder = "D:\Desktop\New folder\temp"
Move-Item -path $filename -Destination $archiveFolder

No error returned and the file was moved. Are you enclosing the filename with double quotes?

  • Related