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.
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
ToSession can use to achieve this target of copying one file
server X to the next
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
$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.
Several issues can arise between establishing the connection once the link is correctly set up.
Copy-Item command the
ToSession commands can use to achieve our target of copying a file from one server to the next.
Related Article - PowerShell Copy
- Copy and Paste From a Variable in PowerShell
- Copy-Item Excluding Folder in PowerShell
- PowerShell to Copy Files Over the Network
- Copy Files and Folders in PowerShell