Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: Copy-Item Cannot find path

I'm trying to get PowerShell to copy files from a remote computer (on which I have admin rights through AD) to the local computer. It fails in the strangest place. Here's a snippet of the script:

    $configs = Get-ChildItem -Recurse -ErrorAction SilentlyContinue -Filter "*.config" $serverUNCPath 
foreach($config in $configs){
    $config_target_dir = $dest.Path + $config.Directory.FullName.Replace($serverUNCPath,"")
    if(Test-Path -Path $config_target_dir){
        Copy-Item $config -Destination  $config_target_dir
    }
}

It fails with the message

Cannot find path 'D:\ServerDeploy\TestMachine1\website\web.config' because it does not exist.
At :line:39 char:12
+           Copy-Item <<<<  $config -Destination  $config_target_dir

The path D:\ServerDeploy\TestMachine1\website exists. I'm going mad over this.

What can I do to fix it?

like image 389
AndreasKnudsen Avatar asked Sep 03 '09 07:09

AndreasKnudsen


People also ask

How do I copy a path in PowerShell?

Copy-Item cmdlet is used to copy a directory by passing the path of the directory to be copied and destination path where the folder is to be copied.

Could not find the part of the path in PowerShell?

Just use the function SHGetFolderPath, which returns the path to whatever folder you want.

How do I copy a file from one location to another in PowerShell?

To copy items in PowerShell, one needs to use the Copy-Item cmdlet. When you use the Copy-Item, you need to provide the source file name and the destination file or folder name. In the below example, we will copy a single file from the D:\Temp to the D:\Temp1 location.

Does xcopy work in PowerShell?

xcopy is the windows command. It works with both PowerShell and cmd as well because it is a system32 utility command.


1 Answers

Eeeeh.... OK?

If I replaced the line

 Copy-Item $config -Destination  $config_target_dir

with

 Copy-Item $config.FullName $config_target_dir

it suddenly magically worked....

What gives?

like image 118
AndreasKnudsen Avatar answered Oct 17 '22 02:10

AndreasKnudsen