How to copy files/folders to the remote location in the PowerShell?


To copy files or folders to or from the remote location, you need to provide the UNC path of the items. For example, here we are going to copy a file from the local computer to a remote computer called Test-PC.

Copy-Item D:\Temp\PowerShellcommands.csv -Destination \Test-
PC\Sharedpath\PScommands.ps1 -PassThru

Here, file PowerShellcommands.csv is copied to the remote computer and renamed it with PSCommands.ps1

You can also copy files from the one shared location to another shared location.

For example,

Copy-Item \Test-PC1\D$\PowerShellcommands.csv -Destination \Test-
PC\Sharedpath\PScommands.ps1 -PassThru

There is another way to copy items to the remote location is, you can use the ToSession parameter. To use the ToSesssion parameter, you need to connect a remote computer using PSRemote computer session and if destination files/folder is in the different domain or the workstation then you need to use the different credentials while remoting.

For example,

$destsession = New-PSSession -ComputerName Test-PC -Credential (Get-Credential)
Copy-Item D:\Temp\PowerShellcommands.csv -ToSession $destsession -PassThru

You can also copy folder(s) with the same method described above.

For example,

$destsession = New-PSSession -ComputerName Test-PC -Credential (Get-Credential)
Copy-Item D:\Temp\* -ToSession $destsession -Force -PassThru

You can also copy files from the one session to another session with the source path should mention with –FromSession parameter, while the destination path should mention with –ToSession parameter.

For example,

$destsession = New-PSSession -ComputerName Test-PC -Credential (Get-Credential)
$sourcesession = New-PSSession -ComputerName Test1-PC -Credential (Get-Credential)
Copy-Item -FromSession $sourcesession -ToSession $destsession -PassThru

Updated on: 12-Mar-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements