Native support for Secure Copy Protocol (SCP) isn't in PowerShell, but that doesn't mean it's not capable. By using a free community module called Posh-SSH, we can transfer files via SCP just as quickly as we can with
Copy-Item
.
The module we need to use is called Posh-SSH. This module is
available on the PowerShell Gallery
and can be installed by running
Install-Module -Name Posh-SSH
. Once installed, you should have a few dozen different commands available around SSH, SCP and SFTP. In this article, we're going to stick with SCP.
Get-SCPFile -ComputerName 'SCPSERVERHERE' -Credential $credential -RemoteFile "/someLinuxFolder/file" -LocalFile 'C:\MyFolder\file'
One item to note, though, is if you're going to be transferring text files from a Windows host to a Linux host, you may run into a situation where the encoding on the file gets screwed up. The text is stored differently on a Linux versus Windows system. If you find the text looks changed when the file is downloaded onto a Windows host, you can create a duplicate copy of the text using the
Out-File PowerShell
command using the Encoding parameter.
Get-Content -Path 'C:\MyFolder\file' | Out-File -Encoding utf8 -Path 'C:\MyFolder\fileWindows'
As with downloading, we can also go the other way and upload files to a remote Linux host, as well, via SCP. For these duties, we can use
Set-ScpFolder
and
Set-ScpFile
. These commands work the same as the
Get
commands but merely send files rather than retrieving them.
Here are some examples of sending a folder or a single file to a remote host via SCP. You can see that the parameters are nearly identical, which makes it easy to switch back and forth between downloading and uploading.
Set-SCPFile -ComputerName 'SCPSERVERHERE' -Credential $credential -RemotePath '/someLinuxFolder/file' -LocalFile 'C:\MyFolder\file'
Set-SCPFolder -ComputerName 'SCPSERVERHERE' -Credential $credential -LocalFolder 'C:\MyFolder' -RemoteFolder '/someLinuxFolder'
Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular
TechSnips
e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at
adamtheautomator.com
, connect on
LinkedIn
or follow him on Twitter at
@adbertram
or the TechSnips Twitter account
@techsnips_io
.
1105 Media Inc
. See our
Privacy Policy
,
Cookie Policy
and
Terms of Use
.
CA: Do Not Sell My Personal Info
Problems? Questions? Feedback? E-mail us.