PowerShell Command to Copy File on Remote Machine

powershell

I have a requirement to copy file from local machine to remote machine using PowerShell. I can copy the file to remote computer using following command:

copy-item -Path d:\Shared\test.txt -Destination \\server1\Shared

the above command uses network share path to copy the file. I don't want to use network share option as the folder will not be shared on the remote machine. I tried following commands but not working.

copy-item -Path d:\Shared\test.txt -Destination \\server1\c$\Shared

Invoke-Command -ComputerName \\server -ScriptBlock {
  copy-item -Path D:\Shared\test.txt -Destination C:\Shared
}

Please let me know how to make it working without using UNC path. I have full permissions on that folder on the remote machine.

Best Answer

Quickest way I found to this, since the account being used is Administrator, is to do the following:

New-PSDrive -Name X -PSProvider FileSystem -Root \\MyRemoteServer\c$\My\Folder\Somewhere\
cd X:\
cp ~\Desktop\MyFile.txt .\
## Important, need to exit out of X:\ for unmouting share
cd c:\
Remove-PSDrive X

Works every time.