Search code examples
powershellcopy-item

Powershell Copy-Item not copying even when folder exists


I have two consecutive calls to Copy-Item but the second one is not copying the file. Both are copying the same file to two different folders. Both folders exist. But the second one never copies. I tried commenting out the first one, but the second one still does not copy.

$deliveryFolder = "C:\Users\administrator.GTCS\Desktop\Delivery\1.5.6.1140\"    
$localFolder = "C:\WebServerGateway\"    
$fileSharingServerManifestFileName = "GTCS_GCS_FileSharingServer_Manifest.xml"

Copy-item -Path $localFolder$fileSharingServerManifestFileName -Destination $deliveryFolder"GcsData\Configurations\Platforms\DEV\Web Server Gateway" -Force

Copy-item -Path $localFolder$fileSharingServerManifestFileName -Destination $deliveryFolder"GcsData\Configurations\Platforms\INT\Web Server Gateway" -Force

Solution

  • If you are going to combine strings in this manner you need to wrap them in double quotes, to expand the variables:

    $deliveryFolder = "C:\Users\administrator.GTCS\Desktop\Delivery\1.5.6.1140\"    
    $localFolder = "C:\WebServerGateway\"    
    $fileSharingServerManifestFileName = "GTCS_GCS_FileSharingServer_Manifest.xml"
    
        Copy-item -Path "$localFolder$fileSharingServerManifestFileName" -Destination "$($deliveryFolder)GcsData\Configurations\Platforms\DEV\Web Server Gateway" -Force
        
        Copy-item -Path "$localFolder$fileSharingServerManifestFileName" -Destination "$($deliveryFolder)GcsData\Configurations\Platforms\INT\Web Server Gateway" -Force
    

    Notice I uses a subexpression $() in the -Destination argument. Because in your case the parser may have trouble determining where the variable name ends, therefore the string may not expand properly.

    That said, neither of the arguments are particularly pretty/readable so I'd advise against this syntax. concatenating or better yet using Join-Path to derive the arguments are better choices:

    $Path = $localFolder + $fileSharingServerManifestFileName
    $Dest = $deliveryFolder + "GcsData\Configurations\Platforms\INT\Web Server Gateway"
    

    Note: Concatenation forces you to pay attention to backslashes separating what would be the -Parent/-Child arguments in Join-Path

    Or:

    $Path = Join-Path $localFolder $fileSharingServerManifestFileName
    $Dest = Join-Path $deliveryFolder "GcsData\Configurations\Platforms\INT\Web Server Gateway"
    

    Then simply modify the commands:

    Copy-item -Path $Path -Destination $Dest -Force
    Copy-item -Path $Path -Destination $Dest -Force