santosh kumar patro
santosh kumar patro

Reputation: 8201

Copy-Item is not working for destination having whitespace in the path variable

I am trying to run the below powershell script from the PowerGUI editor

$windowsServicePath = (gwmi win32_service|?{$_.name -eq "Test Windows Service"}).pathname

$windowsServicePath = $windowsServicePath.Replace("\TestWindowService.exe","")

Write-Host $windowsServicePath

$source = "C:\\Temp\\Configs\\App.DEVINT.config"

Copy-Item -path "$source" -destination $windowsServicePath -recurse -verbose;

On running the above powershell script using the PowerGUI I am getting the below error:

Copy-Item : Cannot find drive. A drive with the name '"C' does not exist.
At C:\Users\Test\Desktop\Powershell Details\Automation Scripts\Test-Powers.ps1:41 char:10
+ Copy-Item <<<<  -path "$source" -destination $windowsSerivcePath -recurse -verbose;
    + CategoryInfo          : ObjectNotFound: ("C:String) [Copy-Item], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

The $windowsServicePath value is C:\Program Files (x86)\Default Company Name\TestWindowsService

But if I update the Copy-Item command with the below code then it works for me

Copy-Item -path "$source" -destination "C:\Program Files (x86)\Default Company Name\TestWindowsService" -recurse -verbose;

Can anyone help me to resolve the above issue?

Upvotes: 1

Views: 1417

Answers (1)

xXhRQ8sD2L7Z
xXhRQ8sD2L7Z

Reputation: 1716

I think the $windowsServicePath value is actually "C:\Program Files (x86)\Default Company Name\TestWindowsService" (with quotes). Can you remove them and pass that to Copy-Item

Upvotes: 1

Related Questions