Copying Files From Local To Remote Using PowerShell

Search for a command to run...

No comments yet. Be the first to comment.
We will be Exploring the PowerShell through this blog series. from basic concepts to advanced scripting and automation. With real-world examples and insightful explanations.
In this tutorial, we will develop a custom QR code generator API and then integrate it into a basic website. We'll build the API using Node.js and deploy it on AWS Lambda. The API endpoints will be established through API Gateway, and the generated ...

In the Field of IT, Automation and Configuration management have become indispensable. As systems grow in complexity, the need to manage and provision them in a reproducible and scalable manner is crucial. Here Ansible can help it's a simple, agentle...

Ansible is a powerful automation tool. When automating various tasks, you may need to utilize sensitive information such as passwords, API keys, and SSH keys. Storing this data in plain text can pose security risks. To effectively manage such sensiti...

Amazon Web Services (AWS) offers a scalable environment for deploying applications, but as the infrastructure grows, so does the complexity of managing it. Organizations need an easy way to manage this infrastructure. they are using different tools t...

Ansible is a powerful, easy-to-use and easy-to-learn configuration management tool. If you are trying to learn Ansible then the first hurdle for you will be setting up a lab environment to practice Ansible. In this article, we are going to create a l...

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
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.
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
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.