Product and service reviews are conducted independently by our editorial team, but we sometimes make money when you click on links. Learn more.
 

How To Send Files Over WinRM With PowerShell

How To Send Files Over WinRM With PowerShell
By

In this tutorial, we'll go over how to send files over WinRM using the new WMF v5 way and we will touch on how to get this done with down-level versions of PowerShell as well.

Have you ever needed to copy a file to a remote computer and found that TCP/445 (SMB) was blocked? That file copy isn't going to happen. This is common in DMZ environments, for example. Perhaps you're already using PowerShell remoting (PSRP) to manage those Windows systems. If so, you're in luck! You can still get files from point A to point B by leveraging the WinRM sessions you're already using. We do this by breaking apart files one a time, streaming their contents over a WinRM session and bringing all the bits back together at the other end.

Prior to the Windows Management Framework (WMF) v5, this was possible but required dozens of lines of code and wasn't easy to comprehend. However, as of WMF v5, Microsoft now includes the functionality out of the box in the form of a couple new parameters to the Copy-Item cmdlet: ToSession and FromSession.

MORE: All PowerShell Tutorials

Sending Files Over WinRM With WMF v5

To send files over a WinRM session in WMF v5 we use the Copy-Item cmdlet. Before we send the file over the session we must first build the session. We do this by using New-PSSession. In this example, we're establishing a remote session to the MEMBERSRV1 computer.

$session = New-PSSession -ComputerName MEMBERSRV1

Next, we have a filed called C:test.xml that we'd like copied to the root of the C: on MEMBERSRV1. To copy this file over, we will call the Copy-Item cmdlet and use the ToSession parameter.

Copy-Item C:test.xml -Destination 'C:' -ToSession $session

That's it! Our test.xml file now resides at C:test.xml on my MEMBERSRV1 computer.

Sending Files Over WinRM Using A Send-File Function

The previous method was using WMF v5. Now let's see how the same task can be done with down-level versions. Fortunately, we've done most of the hard work for you here in the form of a Send-File function. The script that holds this function can be downloaded from Github. To use the it, download the script and dot source the function inside of the script into your PowerShell session. This will allow you to call the Send-File function.

Once you have the function available to you, the method to copy the file to the remote computer is very similar to WMF v5. You first need to establish a PSSession.

$session = New-PSSession -ComputerName MEMBERSRV1

Next, you simply call the Send-File instead of Copy-Item to copy the file from the source computer to the remote computer through the open session.

Send-File -Path C:test.xml -Destination C: -Session $session

Send-File was designed to copy folders as well as files and has the ability to do more advanced things. However, we will let you read the help content provided in the script to investigate those more advanced features.

Finally, whenever you create a persistent session, like New-PSSession does, it's always a good idea to terminate that session. To do this, we use the Remove-PSSession cmdlet.

Remove-PSSession –Session $session

This will terminate the session both on the client as well as the server and get your PowerShell session back to the state it was in prior to establishing the session in the first place.

The next time you need to copy files to remote computers remember that you always have WinRM available!

RELATED: