Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use spaces in a fully qualified pathname in Powershell?

Tags:

powershell

I have a script that copies a number of files from different sources to a single directory for backup. The only step of the script the errors out has a space in both the path and file names: \\server\Network Shares\Transfer\tu3\tu3 Code.mdb

I get the error copy-item : Cannot find path '\\server\Network Shares\Transfer\tu3\tu3 Code.mdb' because it does not exist. and I'm assuming it's because of the spaces in either the path or filename. Does PowerShell allow spaces in a fully qualified path? If not, how can I get at the file?

Here's the relevant code (My$Destis defined as a global variable for the script):

 $TU3CodeUpdatedPathname = "\\server\Network Shares\Transfer\tu3\"
 $TU3CodeUpdatedFilename = "tu3 Code.mdb"
 $TU3CodeUpdated         = $TU3CodeUpdatedPathname + $TU3CodeUpdatedFilename
 #
 $Source = $TU3CodeUpdated
 $Dest   = $VMShareSpacePathname
 #
 copy-item $Source $Dest
like image 675
dwwilson66 Avatar asked Dec 14 '22 17:12

dwwilson66


2 Answers

Try being more explicit, and wrap the parameter values in quotes. Adding -Verbose might help with debugging. If it's complaining the file doesn't exist, maybe double check that the file is indeed accessible when your script runs under the account, if it's not the same as your user account.

  Copy-Item -Path "$Source" -Destination "$Dest"
like image 170
Stephen Connolly Avatar answered Mar 22 '23 23:03

Stephen Connolly


Just to ensure, you might have mixed up the variable names TU3/HS3?

$TU3CodeUpdatedPathname = "\\server\Network Shares\Transfer\tu3\"
$TU3CodeUpdatedFilename = "tu3 Code.mdb"
$TU3CodeUpdated         = Join-Path -Path $TU3CodeUpdatedPathname -ChildPath $TU3CodeUpdatedFilename

Otherwise I can't see anything wrong with your code. Spaces are just fine within quotes as you did write it.

I would guess the running user from the script does not have access rights to the file/share. This post might help in that case.

like image 45
4 revs Avatar answered Mar 23 '23 01:03

4 revs