Pre-requisites
This section enlists a few necessary steps that must be carried out to establish a remote connection. Once the remote connection is established, one can copy files to remote computers.
PowerShell remoting must be enabled on a remote machine and you may execute the following command in this regard. There is a possibility that you may encounter the error as shown in the image below:
To get rid of this error, you have to use the below-stated command:
Configure the trusted hosts settings on both computers by issuing the following command:
In our case the remote host is “ADNAN”:
After adding the remote computer name to the trusted host, use the following commands to restart the WinRM service and test the remote computer name:
> Test-WsMan @<Remotecomputername>
The host is ADNAN in our case:
Once the set of prerequisites is accomplished, you can navigate to the upcoming section for further proceedings of this guide.
How to copy files to remote computers
The steps provided below guide you to create a PowerShell session and execute several commands to copy files to remote computers.
Step 1: Firstly, create a session by using the New-PSSession cmdlet of PowerShell as shown below.
After creating the remote session, you can connect to it by using the Enter-PSSession cmdlet of PowerShell as we did here:
Note: By default, the OneDrive\Documents of the remote host is the working directory. You can use cd.. to fall back to the previous directory of giving the complete path to shift the working directory to that specific one. We have used cd.. thus the next step execution is performed on C:\Users\adnan\OneDrive:
Step 2: Once the session is created successfully, you can copy the file using the Copy-Item cmdlet. In our case, the following command copies a text file to the C:\ drive of the remote computer:
Step 3: After copying the file, verify it by executing the following command. The Invoke-Command searches for the linuxhint.txt file in the C:\ directory and prints the basic information of the file:
If the file is copied successfully, you will observe the information of the file being printed. However, if you have executed Step 3 and could not get the result of Get-ChildItem then you have to look for mistakes or re-execute all the steps with a new session.
Bonus Tip
After dealing with the remote computer, you may need to exit your shell from the remote computer access. To do so, you must pass the session with the Exit-PSSession cmdlet in the following way:
Conclusion
PowerShell provides extensive support for file handling on your machine. In this article, an informative guide is provided to copy files to a remote computer. To interact with a remote computer, you have to create a remote session for that. Apart from learning to copy files to a remote computer, you have learned to create a remote session as well. Moreover, we have also provided the command to copy files from remote to local computer.