dwwilson66
dwwilson66

Reputation: 7066

How can I use spaces in a fully qualified pathname in 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

Upvotes: 4

Views: 16602

Answers (3)

Manikanta
Manikanta

Reputation: 11

This worked for me to copy folder with space in its name. I am using powershell 4.0

$Source = "D:\test\Test cases"
$Dest = "D:\bck\Test cases"
Copy-Item -Path "$Source" "$Dest" -Recurse

Upvotes: 1

Dennie
Dennie

Reputation: 163

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.

Upvotes: 2

Stephen Connolly
Stephen Connolly

Reputation: 1639

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"

Upvotes: 9

Related Questions