Copying Files From Local To Remote Using PowerShell

Copying Files From Local To Remote Using PowerShell

ยท

2 min read

Let's say you want to copy files/folders from a local device to one or more remote Windows servers. We have lots of ways to do this but in this article, we will be looking into how to do that using PowerShell. In your local and remote at both places, you need to enable PowerShell remoting.

Step 1: Check and set the execution policy

Get-ExecutionPolicy

if the exception policy is not RemoteSigned set it to RemoteSigned.

Set-ExecutionPolicy RemoteSigned

Step 2: Enable PowerShell remoting

Please keep in mind, When the SkipNetworkProfileCheck parameter is specified, it bypasses the check for network profiles during the configuration process. This allows PowerShell remoting to be enabled even if the network profile is not classified as private or domain.

Enable-PSremoting -SkipNetworkProfileCheck

Now to the main part

Method 1: Using Copy-Item with session

The most simple and best way. just use the ToSession parameter provided within copy-item.

$sourcePath = "C:\temp\files"
$destinationPath = "C:\temp\files"
$serverName = "<server name>"
$username = "<username>"

$credentail = Get-Credential -Message "Enter Credentials" -UserName $username
$session = New-PSSession -ComputerName $serverName -Credential $credentail

Copy-Item -Path $sourcePath -Destination $destinationPath -Recurse -ToSession $session

in the above script, we created a PowerShell session to that remote Windows server and then we are copying the whole folder from source to destination.

Method 2: Using Copy-Item with PS Drive

Here we are creating a ps-drive and then doing the copying part. this method helps to interact with the remote location in a way that resembles working with a local drive.

$sourcePath = "C:\temp\files"
$destinationPath = "C:\temp\files"
$serverName = "<server name>"
$username = "<username>"
$driveName = "R"

$credentail = Get-Credential -Message "Enter Credentials" -UserName $username

New-PSDrive -Name $driveName -PSProvider FileSystem -Root $destinationPath -Credential $credentail

Copy-Item -Path $sourcePath -Destination $driveName:\ -Recurse

Remove-PSDrive -Name $driveName

Method 3: Using Robocopy

Robocopy (Robust File Copy) is a command-line utility built into Windows that provides advanced options for copying files and directories.

While using robocopy easier way is to create a local drive and then use that for copying files.

keep in mind drive name should be a single character like A, R, X etc. Not names like "newDrive", "test" etc.

$sourcePath = "C:\temp\files"
$destinationPath = "C:\temp\files"
$serverName = "<server name>"
$username = "<username>"
$driveName = "R"

$credentail = Get-Credential -Message "Enter Credentials" -UserName $username

robocopy $sourcePath $driveName:\  /E /Z /COPYALL /R:3 /W:1

Remove-PSDrive -Name $driveName

Meaning of copy options we have passed

/E: copies subdirectories, including empty ones.

/Z: enables restartable mode.

/COPYALL: copies all file information.

/R:3: specifies 3 retries on failed copies.

/W:1: sets the wait time between retries to 1 second.


ย