RMcLean
RMcLean

Reputation: 13

Powershell move files with special characters

How can I move a file with special characters in it? I am not allowed to rename the file.

My file is: File.Server.Windows.2003.[SP2].01232005.txt

# dFold = The destination folder in the format of \\drive\folder\SubFolder\   
# tDir = Root Target Directory on NAS
# sFold = The name of the subfolder that the files will go into
$dFold = "$tDir$sFold"

# sDir = Source Directory
# $File = Original file name as seen in the source Directory
Move-Item -Path $sDir$File -Destination $dFold -force

When I try to execute the above code it does not move the file. I can add some Write-Host statements and it says it moves the file, but it really don't.

Write-Host "Now moving " $File "to " $dFold"\"
  Move-Item -Path $sDir$File -Destination $dFold -force

  # Now we just write put what went where or not          
  Write-Host $File "Was Moved to:" $dFold

Output:

Now moving  File.Server.Windows.2003.[SP2].01232005.txt to  \\NAS\Inventory\Servers\  
File.Server.Windows.2003.[SP2].01232005.txt Was Moved to: \\NAS\Inventory\Servers  

Upvotes: 1

Views: 2294

Answers (1)

Micky Balladelli
Micky Balladelli

Reputation: 10011

Did you try:

Move-Item -LiteralPath $sDir$File -Destination $dFold

Move-Item allows wildcard matches when using the -Path parameter, so a substring [SP2] is interpreted as a single character 'S', 'P', or '2' instead of the string '[SP2]'. Using the parameter -LiteralPath instead of -Path prevents that.

Upvotes: 5

Related Questions