How to Copy File to Remote Computer in PowerShell
In Linux-like systems, many different tools can run through Command-Line Interfaces. However, for Windows and Mac users, PowerShell by Microsoft is used to bridge this gap between Windows, Mac users, and Linux-like systems.
PowerShell offers a Command-line interface for Scripting purposes, automation requirements, and CI/CD systems management
.
Windows version of PowerShell can be installed in Windows computers and Mac version in Mac Computers, while they support Linux.
Several versions have been released since version 1 of the PowerShell. The earliest version up to version 5 would be updated for you by the Windows Operating System itself.
Therefore, if you are using a Windows version of Windows 7 or newer, you would be having PowerShell 5
or later versions.
Through this article, we will be discussing the methods of using PowerShell to copy files to a remote computer. It is a frequent question many users ask and a confusing point for many.
Hence We will elaborate on commands and methods to achieve this requirement most efficiently.
Use PowerShell Tosession
to Copy Files to a Remote Computer
We have to discuss an issue related to moving files from one Server session to another server.
Let us call Server X
and Server Y
. If the user is connected to Server X
and wants to send a file to Server Y
. You can use PowerShell to achieve the target.
Two commands from FromSession
and ToSession
can use to achieve this target of copying one file server X
to the next server Y
.
You have to connect to session Y
. You can initiate session Y
from session X
by the following command.
However, remoting is only possible in the Windows PowerShell versions above version 5, starting from version 5. As mentioned earlier, you must already have version 5.
It would not be an issue for you. Here the New-PSSession
provides the command to create a new PowerShell session in the remote computer. The Y
is the remote server Y
.
$SessionY = New-PSSession Y
Then the following command should execute with correct file paths.
Copy-Item -FromSession $SessionY C:\Users\Agni\dummydata.txt -Destination C:\dummydata.txt
Further, it is unnecessary for both the computers to have the PowerShell installed. It would be best if you had PowerShell in the sending computer.
There are other methods to do this. But, they are all based on the proper connection between two servers.
Conclusion
Several issues can arise between establishing the connection once the link is correctly set up.
The Copy-Item
command the FromSession
and ToSession
commands can use to achieve our target of copying a file from one server to the next.
Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.