Powershell

PowerShell Copy File to Remote Computer

In computing, file handling processes are one of the frequently performed actions. PowerShell allows you to share files within current sessions and remote sessions. The remote sessions refer to remote computers. Whenever a PowerShell is started,  a new session is initiated automatically. Here, we have to deal with remote computers using PowerShell and it is observed that the automatic creation of sessions does not support remote computers. The execution of PowerShell commands on a remote computer requires PowerShell sessions. This article provides the procedural guide to copy files to a remote computer.

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:

> Enable-PSRemoting -Force -Verbose

To get rid of this error, you have to use the below-stated command:

> Set-WsManQuickConfig

Configure the trusted hosts settings on both computers by issuing the following command:

> winrm set winrm/config/client  '@{TrustedHosts="<enter-remote-computer-name-here>"}'

In our case the remote host is “ADNAN”:

> winrm set winrm/config/client '@{TrustedHosts="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:

> Restart-Service WinRM
> Test-WsMan @<Remotecomputername>

The host is ADNAN in our case:

> Test-WsMan @ADNAN

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.

> $SESSION= New-PSSession -ComputerName <Remote computer name> -Cred $credential

After creating the remote session, you can connect to it by using the Enter-PSSession cmdlet of PowerShell as we did here:

> Enter-PSSession $SESSION

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:

> Copy-Item –Path "F:\linuxhint.txt" –Destination "C:"

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:

> Invoke-Command -ScriptBlock {Get-ChildItem -Path "C:\linuxhint.txt"}

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:

> Exit-PSSession <session-name>

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.

About the author

Adnan Shabbir