How To Transfer Files Via SCP With PowerShell

As an experienced sysadmin, you will eventually work in multiple operating systems and often need to copy files from a server to a Linux Server securely. In Linux, Secure Copy Protocol (SCP) is native to the Operating System. For Windows systems, you usually have to install separate tools to transfer files to and from a Linux server securely.

What about PowerShell? Native support for SCP isn’t in PowerShell. However, with the extensibility of PowerShell, we can download a module for it. Using Posh-SSH, adds SCP and we can transfer files just as quickly as we can with Copy-Item.

Prerequisites include: Linux Server POSH-SSH Module

There were going to be working with a couple of commandlets that are available in the posh SSH module. I have provided a link up at top so that you can download it. I've already set up at Linux Server in my virtual labs that we have something to upload and download files from and it'll be running to command on Windows 10 machine using the latest version of powerShell and the first thing I want to do is store. My credentials that will help with stopping, prompting of my credentials. Every time I run the command. I've already done that to save some time. You said not let's log into the Linux server and then check the directory's so that do that. We're going to click on the split terminal icon. Go ahead and split the terminal at all SSH to the server now. I'm using IP address that could be a server name. I just don't have a DNS server setup in my lab, so let's go ahead and log into that. I'll provide my password going to see. I'm logged into the Linux server. Let's do a directory search and see have no directory's listed there. Now I've got some test files if you look on the left directory tree. I have a folder called SCP copy the 2 test files test dot text and test. One dot text so let's upload the whole folder and the files within it. So the command that we're going to use at the set. SCP folder and we want to pass it a computer name. I'm using the IP address 'cause. I don't have DNS setup in my lab and then it will pass the credentials. The local folder for here is the SCP Copy and then the remote folder that you want to move it, too, so on the next server. We're going to be using from windows and that provided the verbose command, so that we see with that man. You're doing so, let's go ahead and execute that line. Do you see that we've got the files uploaded in that let's check our directories and will do a directory search on this. We now have a new directory called from windows. Let's go ahead and do another directory listing you see we now have 2 files that have been copied so let's go ahead and download those same files and folders. The first I want to do is delete. These 2 files from my windows machine and will go ahead and download the files from the Linux Server Command is very similar, but this time, we're going to be using get. SCP folder again will pass with the computer name or the IP address the credentials and the local folder that you want to copy it to. And then the remote folder were the files were sitting. And let's go ahead and execute that command. You see this command actually failed you know why look at the directory listing here between the first time I ran it, which was a capital W and the second time I ran it, which was a lowercase W. Now I must admit I did that on purpose to demonstrate the fact that Linux is a case sensitive file system. So let's go ahead and change it back up to an uppercase W and will run the command again. And you can see on the directory tree on the left at the 2 files have been copied OK. So now that we've got the folders taking care of. Let's upload and download an individual file so let's go ahead and create a test file and that's what I'm doing here is just a plain text file going to echo it into a text file dot tech. And you should see it appear on the Left Directory Tree and it does you see appear this is the file. I just created so the same as with the folders. We're going to be using the set. SCP file which means we're going to be uploading it to the computer, which is the IP address of my Linux machine again pass through credentials and then the remote path where I want the files and then the actual file and then again, I'm using verbose command, so that we can see with the command is doing. Let's go ahead and execute the outline. Now let's go over to my Linux machine and see if it copied the file will do a directory listing. Did you see did copy the file to my Linux Machine? Now let's download it. It was go ahead and delete this file for my windows machine and will go ahead and download it again from the Linux machine again. I want to remind you that Linux is a case sensitive file system so you need to be sure you're spelling. It not only correctly, but the case are the same across the systems so again. It's get SCP file the computer name. Again, I'm using the IP address. Passing the credentials the remote file that I want to download from the Linux Server and the local file which is the Windows 10 box, it on downloading it to let's go ahead and execute that command. As you can see from the directory tree on the left. It did copy that file from the machine. It's as simple as that thanks for watching.