PowerShell command to copy a file to a remote machine

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

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

the above command uses a network shared path to copy a file. I do not want to use a network resource, since the folder will not be used on the remote computer. I tried the following commands but did not work.

 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 work without using a UNC path. I have full permissions on this folder on the remote machine.

+9
source share
6 answers

The fastest way I found this, since the account used is the Administrator, is as follows:

 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 

It works every time.

+6
source

You must have a shared folder that allows you to copy files from one host to another, or to a remote host if you want to click a file:

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

or on localhost 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 administrator rights on this particular computer.

+4
source

If there is no shared resource available, you need to make the content of the file itself as a script argument:

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

Please note that C: \ myfolder \ result.txt is located on the remote computer

+1
source

Here is a script that worked for me for small files. Run as administrator.

 #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 } 
0
source

Powershell 5 (Windows Server 2016)

Also available 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
source

Source: https://habr.com/ru/post/980359/


All Articles