How to Copy Files to Remote Computer With Powershell

Easy file copy scripts on PowerShell

by Afam Onyimadu
Afam Onyimadu
Afam Onyimadu
Editor
Afam is a geek and the go-to among his peers for computer solutions. He has a wealth of experience with Windows operating systems, dating back to his introduction... read more
Reviewed by Alex Serban
Alex Serban
Alex Serban
Windows Server & Networking Expert
After moving away from the corporate work-style, Alex has found rewards in a lifestyle of constant analysis, team coordination and pestering his colleagues. Holding an MCSA Windows Server... read more
Affiliate Disclosure
  • PowerShell is a robust Microsoft scripting terminal used to perform multiple operations.
  • You can transfer single and multiple files to remote machines using the proper scripts.

XINSTALL BY CLICKING THE DOWNLOAD FILE
To fix various PC problems, we recommend Restoro PC Repair Tool:
This software will repair common computer errors, protect you from file loss, malware, hardware failure and optimize your PC for maximum performance. Fix PC issues and remove viruses now in 3 easy steps:

  1. Download Restoro PC Repair Tool that comes with Patented Technologies (patent available here).
  2. Click Start Scan to find Windows issues that could be causing PC problems.
  3. Click Repair All to fix issues affecting your computer's security and performance
  • Restoro has been downloaded by 0 readers this month.

You must have found this guide because you want to know how to copy a file to a remote computer using PowerShell.

The good news is that we understand the need for file sharing and have put together every detail you need to know in this article.

So without much ado, let us get to it.

Why should I transfer files using PowerShell?

PowerShell may be used to transfer files for a number of reasons:

  • Speed: When uploading a lot of data across a network, PowerShell may be quicker than utilizing GUI-based solutions.
  • Automation: You may transmit files automatically on a schedule or in reaction to certain circumstances by using PowerShell scripts that have been automated.
  • Versatility: File transfers utilizing FTP, FTPS, SFTP, and SCP are all possible with PowerShell.
  • Handling errors: PowerShell has strong error-handling features that might be helpful when transferring files to make sure the transfer went well.
  • Reporting: PowerShell is capable of generating thorough reports on the transfer procedure, which include the total volume of data transferred, the number of files transferred, and any mistakes that had occurred.

In general, managing file transfers using PowerShell may be practical and effective, especially in a professional setting.

Now here is how to copy files to remote machines using the tool.

How do I copy a file to a remote computer using PowerShell?

In this article

1. Copy a file from a remote computer using PowerShell

1.1 Set up the remote connection

  1. Open PowerShell with admin rights, input the command below, and hit Enter.
    Enable-PSRemoting -Force -Verbose
    powershell copy file to remote computer
  2. If you get the error as displayed in the image above, enter the following command; if not, skip this step:
    Set-WsManQuickConfig
  3. Use the following command to configure your trusted host, substituting AFAM with the name of the computer:
    winrm set winrm/config/client '@{TrustedHosts="AFAM"}'
    powershell copy file to remote computer
  4. Use these shell scripts to restart your WinRM service and test the remote computer name after you have added the remote machine name to the trusted host (remember to substitute AFAM for your computer name):
    Restart-Service WinRM
    Test-WsMan @AFAM

Expert tip:

SPONSORED

Some PC issues are hard to tackle, especially when it comes to corrupted repositories or missing Windows files. If you are having troubles fixing an error, your system may be partially broken.
We recommend installing Restoro, a tool that will scan your machine and identify what the fault is.
Click here to download and start repairing.

A few actions to create a remote connection are listed above. One may copy files to distant machines after establishing the remote connection.

Note that on a remote system, PowerShell remoting must be enabled. To do this, use the steps highlighted above.

1.2 Use Copy-Item to copy the file

  1. Use the New-PSSession cmdlet to start a session using the command below (remember to replace AFAM with the PC name).
    $SESSION= New-PSSession -Computer 'AFAM' -Cred $credential
    powershell copy file to remote computer
  2. Connect to the newly created session using the command below (note that you can use the cd command to change the directory if needed):
    Enter-PSSession $SESSION
  3. Copy the file using the command below (remember to replace initial and final location with the actual paths you need the file copied from and to):
    Copy-Item –Path "initial location" –Destination "final location"
  4. You may need to quit the shell from the remote machine access after interacting with the remote machine. To do this, you must terminate the session using the Exit-PSSession cmdlet as follows:
    Exit-PSSession

2. Copy multiple files to a remote computer using PowerShell

  1. Follow all the steps mentioned in solution 1.1
  2. Declare some variables using the script below (include actual values for the username, password, and both paths):
    $username = "username"
    $password = "password"
    $remote_path = "\remote_computer\c$\path\to\destination"
    $local_path = "C:\path\to\local\files"
  3. Next, create a credential object using the script below:
    $credential = New-Object System.Management.Automation.PSCredential ($username, (ConvertTo-SecureString $password -AsPlainText -Force))
  4. Use the script below to get the local path that has been declared:
    $files = Get-ChildItem $local_path
  5. Finally, copy each file to the remote computer using the foreach function:
    foreach ($file in $files) {
    Copy-Item -Path $file.FullName -Destination $remote_path -Credential $credential
    }

This will copy all the files in the local directory specified by $local_path to the destination directory on the remote computer specified by $remote_path. You will need to provide the correct values for $username, $password, $remote_path, and $local_path.

There you have it. These are some of the best ways to copy files to a remote computer using MS PowerShell.

PowerShell is a very robust tool, and with some knowledge of scripting, there is a lot you may be able to achieve using the tool. You can copy files from and to remote computers using the tool.

Please let us know in the comments below if this guide has been helpful.

Still having issues? Fix them with this tool:

SPONSORED

If the advices above haven't solved your issue, your PC may experience deeper Windows problems. We recommend downloading this PC Repair tool (rated Great on TrustPilot.com) to easily address them. After installation, simply click the Start Scan button and then press on Repair All.

This article covers:Topics: