1

I am trying to use the powershell Copy-Item command to copy a file to a UNC Path. Unfortunately, the UNC path on this production server has both spaces and an ampersand in the name. I've tried a bunch of things, but haven't had any luck yet. Could you please suggest a way to resolve this issue.

$InvokeExpressionPath = "\\servername\This Folder Has Spaces & Ampersand\Folder"
$TransfersSharePath = Invoke-Expression $InvokeExpressionPath
$TransfersSharePathFile = $InvokeExpressionPath + "\" + $FileName
Copy-Item -Path $CheckFileExists -Destination $TransfersSharePathFile -Force -ErrorAction SilentlyContinue

This is the error message that I get:

Invoke-Expression : At line:1 char:41
+ \\servername\This Folder Has Spaces & Ampersand\Folder
+                                     ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string.
At E:\Scripts\CopyFile_Test.ps1:33 char:27
+     $TransfersSharePath = Invoke-Expression $InvokeExpressionPath
+                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseException
    + FullyQualifiedErrorId : AmpersandNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

If I try to wrap the ampersand in double-double quotes (""&""), I get this error when the code runs.

\\servername\This : The term '\\servername\This' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was 
included, verify that the path is correct and try again.
At line:1 char:1
+ \\servername\This Folder Has Spaces "&" Ampersand\Folder
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\\servername\This:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
DEBUG: This is the value of TransfersSharePathFile - \\servername\This Folder Has Spaces & Ampersand\Folder\file.zip
Copy-Item : Illegal characters in path.
At E:\Scripts\CopyFile_Test.ps1:36 char:5
+     Copy-Item -Path $CheckFileExists -Destination $TransfersSharePathFile -Force ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.CopyItemCommand

Thanks in advance for your help.

JiggidyJoe
  • 489
  • 2
  • 8
  • 21

3 Answers3

1

Thanks very much to AdamL and dread1 for their help. I used both suggestions. For reference, this is what the working code now looks like.

$InvokeExpressionPath = "\\servername\This Folder Has Spaces & Ampersand\Folder"
$TransfersSharePathFile = Join-Path -Path $InvokeExpressionPath -ChildPath $FileName
$TransfersSharePathFile = $InvokeExpressionPath + "\" + $FileName
Copy-Item -Path $CheckFileExists -Destination $TransfersSharePathFile -Force -ErrorAction SilentlyContinue
JiggidyJoe
  • 489
  • 2
  • 8
  • 21
0

Try:

$InvokeExpressionPath = '\\servername\This Folder Has Spaces & Ampersand\Folder'

Should do the job

dread1
  • 134
  • 2
  • OR, if you need to pass ampersand to Invoke-Expression you ca try to use `$InvokeExpressionPath = """\\servername\This Folder Has Spaces & Ampersand\Folder"""` this will do the job with invoke... but later you'll have to remove extra qoute... `$TransfersSharePathFile = $($InvokeExpressionPath -replace """") + "\" +$FileName` – dread1 Jun 30 '20 at 09:32
0

Ampersand or spaces in path are not the problem. You're trying to use Invoke-Expression supplying path as parameter, which is not a valid command. Get rid of $TransfersSharePath line and use what's in $InvokeExpressionPath directly. Just change double quotation marks to single just in case. Also I suggest you create path using Join-Path instead of contatenating the strings.

AdamL
  • 12,421
  • 5
  • 50
  • 74