11
votes

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.

6
What exactly does "not working" mean? Did you get an error message? No file was copied at all? File was copied into wrong location? Wrong file was copied to correct location? Something else happened?vonPryz
Also when running the Invoke command the paths would be relative to the running server so then D:\Shared\test.txt would need to be a UNC pathMatt
I am getting error "Network Path not found".Parveen Kumar
Possible duplicate of Copy File Remotely with Powershellbummi

6 Answers

8
votes

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.

4
votes

You must have a shared folder to be able to copy files from one host to another, either on the remote host if you want to push the file:

Copy-Item -Path D:\folder\test.txt -Destination \\server1\remoteshare\

or on the local host if you want to pull the file:

Invoke-Command -ComputerName server1 -ScriptBlock {
  Copy-Item -Path \\localcomputer\localshare\test.txt -Destination C:\Shared\
}

Administrative shares (\\server1\c$) can only be used if your account has admin privileges on that particular computer.

3
votes

If there is not an accessible share, you'll have to make the file content itself an argument to the script:

Invoke-Command -ComputerName \\server -ScriptBlock {
  $args[0] | Set-Content  C:\Shared\test.txt
  } -ArgumentList (Get-Content D:\Shared\test.txt -Raw) 
1
votes
Invoke-Command -ComputerName compname -Credential $cred  -ScriptBlock { Get-Content C:\myfolder\result.txt } >>res.txt

Note the C:\myfolder\result.txt is on the remote computer

1
votes

Powershell 5 (Windows Server 2016)

Also downloadable for earlier versions of Windows. -ToSession can also be used.

$b = New-PSSession B
Copy-Item -FromSession $b C:\Programs\temp\test.txt -Destination C:\Programs\temp\test.txt

Earlier versions of PowerShell

Copy-Item -Path \\serverb\c$\programs\temp\test.txt -Destination \\servera\c$\programs\temp\test.txt
0
votes

Here's a script that worked for me for small files. Run as admin.

#pre 5.0 powershell copy-item to remote computer
Write-Host "Remote copy a file"
$servers = @("server01.dot.com", "server02.dot.com")
foreach($server in $servers) {
$username = 'USERNAME'
$password = 'PASSWORD'
$pw   = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ($username, $pw)
$myfile = [System.IO.File]::ReadAllBytes("C:\Temp\srctest.txt")
$s = New-PSSession -computerName $server -credential $cred
Enter-PSSession $s
Invoke-Command -Session $s -ArgumentList $myfile -Scriptblock {[System.IO.File]::WriteAllBytes("C:\Temp\desttest.txt", $args)}   
Write-Host "Completed"
Remove-PSSession $s
}