OK, the real problem was that I was getting the path from a registry entry and then cleaning it with Regex. The very start of the path had a space in the front which I did not notice the way I was logging. I fixed this problem by using this: #outvar is used to build the path and comes from a registry key | Out-String $outvar = ($outvar -replace '\s','')

$localtruststore = "C:\Users\me\OneDrive\work\scripts\PS\TEST\truststore"
$servers = "SERVER1"

## remotepath is actually set by looking at a registry entry, but I am sure it is coming out like this:
$remotepath = "d$\programname\40\server\Openfire\resources\security"

#### THIS LINE CAUSES THE ERROR - I think just because of the $.
Copy-Item $localtruststore -Destination \\$server\$remotepath -Force

Copy-Item : Illegal characters in path.
At C:\Users\me\OneDrive\work\scripts\PS\TEST\chat_copy_trustore_to_remote.ps1:46 char:11
+     Copy-Item <<<<  $localtruststore -Destination \\$server\$remotepath -Force
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.CopyItemCommand

If I manually put the Destination in, the copy works perfectly, so this must be a simple syntax issue.

I have tried completely building the $destinationpath variable by doing these:

$destinationpath = "\\$server\$remotepath"    
$destinationpath = ("\\{0}\{1}" -f $server,$remotepath)

BOTH of these work and when I write-host the variable I get the correct \server\d$\programe... I still get illegal character in path., char14

I have confirmed that the character referenced in the error is the exact number where the $ symbol is in the destination. it is that char number in that argument.

I ended up just breaking this in to two parts....see below..

This will copy to the admin share for the root of D. Copy-Item -path $localtruststore -Destination \$server\d$ -Force

Then I use this later to move the file on the remote server.. Invoke-command -ComputerName $server { Copy-Item -path D:\truststore -Destination D:\Temp -Force }

What may or may not have complicated this is my Java Keystore files, JKS files, do not have file extensions..

  • (Get-ItemProperty HKCU:\Software\Dropbox -Name Installpath).installpath.trim() # You can use Get-ItemProperty to read the regkey and remove leading and trailing whitespaces with trim(). – megamorf Mar 17 '15 at 17:42

UNC paths start with double backslashes so your command should look like this:

$source = "C:\Users\me\OneDrive\work\scripts\PS\TEST\truststore"
$server = "server1"
$destinationpath = "\\$server\d$\programname\40\server\Openfire\resources\security"

copy-item -path $source -destination $destinationpath -verbose

Or you can do it like this:
$foldershare = "d$\programname\40\server\Openfire\resources\security"
$destinationpath = ("\\{0}\{1}" -f $server,$foldershare)
  • Where would I specify the drive? I need to use a variable for the folder path. In other words, if have a variable for the destination folder, how do I concatenate the server with the d$ share on it? – user1991791 Mar 17 '15 at 13:55
  • the same way megamorf did with $server perhaps? – Frank Thomas Mar 17 '15 at 13:59
  • I added an alternative way for you. – megamorf Mar 17 '15 at 14:00
  • I tried the second one. The variable $destinationpath is being created correctly as \\servername\d$\programname\folder BUT when I go to use it with Copy-Item $localtruststore -Destination $destinationpath -Force I still get the Illegal Characters in path errror – user1991791 Mar 17 '15 at 14:08
  • copy-item D:\temp\debug -Destination "\\$server\$share" -Verbose -Force # works for me – megamorf Mar 17 '15 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.