Logo

dev-resources.site

for different kinds of informations.

Copying Files From Local To Remote Using PowerShell

Published at
10/8/2023
Categories
powershell
automation
copy
robocopy
Author
anujsd
Author
6 person written this
anujsd
open
Copying Files From Local To Remote Using PowerShell

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

Enter fullscreen mode Exit fullscreen mode

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

Set-ExecutionPolicy RemoteSigned

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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.


copy Article's
30 articles in total
Favicon
Essential FFmpeg Recipes for Video Manipulation
Favicon
Unlocking Productivity: The Power of Keyboard Shortcuts
Favicon
Copy file between server and local machine ( from windows to linux server )
Favicon
Python's shutil module for Automated Testing
Favicon
The Pinnacle of Imitation: Master Copy Watches UAE Edition
Favicon
Two updates to the rucken copy-paste utility
Favicon
Docker Commands copy, remove images.
Favicon
How to load JSON data in PostgreSQL with the COPY command
Favicon
Looking forward experience developer
Favicon
OCP - OpenShift Container - Need to Copy Custom CA-Trust Certificates for Proxy Call from Company Domain to Azure
Favicon
Elevate Your Buttons with CopyShareify-js: Copy, Share, and More!
Favicon
Copying Files From Local To Remote Using PowerShell
Favicon
wayland & clipboard
Favicon
How to copy lots of data fast?
Favicon
Copy paste source files to destination with singular and plural replace text in file contents and file paths
Favicon
#010 Jira unformatted / formatted paste - How to paste plain text in Jira tickets | Jira messages - CTRL+SHIFT+V
Favicon
How to copy/paste files/directories into non-existent destination path
Favicon
Copy to Clipboard - Clipboard.js
Favicon
Copying Items to Clipboard Without Using Clipboard API
Favicon
Inspecting the Clipboard (on Linux)
Favicon
COPY progression in YugabyteDB
Favicon
HTML on the Clipboard: Oh, what shall we copy?
Favicon
Copy to Clipboard: The Options
Favicon
Copy to Clipboard: First Cut
Favicon
Bulk load into PostgreSQL / YugabyteDB - psycopg2
Favicon
Copy text to Clipboard using JavaScript
Favicon
How to Copy Array in Java
Favicon
[Dicas do VIM] Copiar, cortar e colar no VIM / NeoVim
Favicon
Creating copy button with JS
Favicon
Windows 10 copy multiple paste - shortcut/multiple paste option

Featured ones: