If you need to transfer files with PowerShell SFTP, here’s how.
The “powershell sftp client” is a command-line tool that allows users to transfer files with the help of PowerShell.
You’re in luck if you need to transfer data securely using SFTP and know a little PowerShell. In this lesson, you’ll learn how to utilize SFTP to deal with files remotely and explore some of the PowerShell SFTP modules available.
Are you not a reader? Take a look at this related video.
Let’s get started!
This will be a hands-on presentation in this course. If you want to follow along, make sure you have the following items on hand:
- A remote SFTP server — The FTP server in Windows Server 2019 will be used in this tutorial.
- You may utilize either Windows PowerShell 5.1 or PowerShell v7+ with PowerShell.
How to Guide Yourself Through a PowerShell 7 Upgrade
Using the Posh-SSH Module to Manage Files through PowerShell SFTP
Posh-SSH is a useful module for working with files through SFTP. To get started, execute Install-Module in PowerShell as an administrator to download and install the module from the PowerShell Gallery. If questioned about an untrusted repository, confirm by typing Y or A.
Posh-SSH -Name Install-Module
Files and Folders are being downloaded from the SSH server.
Let’s look into downloading files now that you’ve installed the module. You must first create a session in order to download files. You’ll need the session to do numerous SFTP actions since it’s a permanent connection.
To start a session, use the Get-Credential command to get a PSCredential object and the New-SFTPSession command to connect to a remote SFTP server. PowerShell will ask you for a username and password when you run the command below.
New-SFTPSession -ComputerName 10.0.0.10 -Credential $SFTPSession = New-SFTPSession -ComputerName 10.0.0.10 -Credential $SFTPSession = New-SFTPSes (Get-Credential)
Credential Prompt for New-SFTPSession
The script will request you to accept the Server SSH Fingerprint if the connection is successful. The server will trust the connection from your client based on this certificate fingerprint.
Output from the cmdlet New-SFTPSession
Run the command below to download a file from the remote server now that you’ve connected to it. The Get-SFTPItem command downloads the file /home/user01/Downloads/test.txt from the distant server to the local computer’s C:temp directory. The SessionId attribute on the SFTPSession object acquired in the previous step is also used in the command below.
$SFTPSession.SessionId -Path /home/user01/Downloads/test.txt -Destination c:temp Get-SFTPItem -SessionId $SFTPSession.SessionId -Path /home/user01/Downloads/test.txt
You may also use the Path argument to download whole directories, such as /home/user01/Downloads.
Files and Folders are uploaded to the SSH server.
Let’s look at how to upload files and directories from your SSH server now that you’ve seen how to download them. The procedure for uploading files is largely similar to that for downloading files, as shown below.
Invoke the Set-SFTPItem command, specifying the session to connect to, the item to upload, and the place to upload the file on the SSH server.
Set-SFTPItem -SessionId $SFTPSession.SessionId -Path C:Temptest.txt -Destination /tmp Set-SFTPItem -SessionId $SFTPSession.SessionId -Path C:Temptest.txt -Destination /tmp
You may even upload whole directories by specifying a folder path in the Path option, such as C:TempTestFolder.
Posh-SSH Session Disconnection
Finally, after you’re done using the Posh-SSH session, use Erase-SFTPSession to disconnect and remove it from memory. This step is optional, but it cleanly disconnects the server’s connection and frees up memory in PowerShell.
-SFTPSession Remove-SFTPSession $SFTPSession
Using the WinSCP Module to Manage SFTP Files
If you don’t want to utilize the Posh-SSH module for any reason, you can always use the WinSCP module instead! Another useful module that comes from the famous WinSCP GUI program is the WinSCP module.
To get started, download and install the Posh-SSH module from the PowerShell Gallery, as shown below.
-Name WinSCP -Install-Module
The Ultimate Guide to the WinSCP Command-Line
Using the WinSCP Module to Examine Files and Folders
You may begin using the module now that it has been installed. First, let’s establish a connection with the server. You must constantly create a connection to the remote server, just as with the Posh-SSH module.
The New-WinSCPSession command will ask you to add the server’s public key to your local certificate repository the first time you try to connect to the SFTP server. This is required in order for the SFTP server to trust your local machine.
To add your key, click Copy key fingerprints to clipboard and paste the fingerprint into a text field. This will be required in the following step. Next, copy and paste the line that looks like this: “ssh-ed25519 255 48:b4:4e:3d:67:ca:25:e4:c7:77:fe:3c:df:ae:d9:b9” into the script as seen below.
Using the WinSCP Module to Inspect Files and Folders
Below is an example script for establishing a connection. This script produces a PSCredential object, which is supplied to the Credential argument of the New-WinSCPSessionOption command, as well as the remote server and protocol sftp.
#Enter credentials to connect to FTP server. $FTPUsername = “UserID” $FTPPwd = “UserPwd” $Password = ConvertTo-SecureString $FTPPwd -AsPlainText -Force $Credential = New-Object System.Management.Automation.PSCredential ($FTPUsername, $Password) #Import WinSCP module Import-Module WinSCP #Create WinSCP session to your FTP server. 10.0.0.3 is the FTP server. $WinSCPSession = PS C:> New-WinSCPSession -SessionOption (New-WinSCPSessionOption -HostName 10.0.0.3 -Protocol Sftp -Credential $Credential -SshHostKeyFingerprint “ssh-ed25519 255 48:b4:4e:3d:67:ca:25:e4:c7:77:fe:3c:df:ae:d9:b9”)
Run the Get-WinSCPChildItem command once you’ve established a connection to the server and specify the session you just created. From the remote server user’s home directory, this command will enumerate all of the files and folders on the remote server.
-WinSCPSession Get-WinSCPChildItem $WinSCPSession
The content of an FTP server is shown.
Use the Path option to only list files in a certain directory, as seen below.
-WinSCPSession Get-WinSCPChildItem $WinSCPSession -Path ‘/Folder2’
The content of an FTP server is shown. in a subfolder
Using the WinSCP Module to Upload Files and Folders
Using the WinSCP module to upload files and folders is similar to using the WinSCP module to list files. Run the Send-WinSCPItem command as indicated below to do this. Send-WinSCPItem uploads a file by supplying the session (WinSCPSession), the local Path of the item to upload, and the RemotePath to where the file should go.
If you don’t use the -RemotePath argument to provide a path, your files and folders will be uploaded to the user’s home directory with whom you authenticated to the server.
-WinSCPSession -Send-WinSCPItem -Path C:TempTestFile.txt -RemotePath ‘/Folder2/’ $WinSCPSession
When the upload is finished, Send-WinSCPItem will show the remote folder path holding the file that was uploaded.
Uploading a file to an FTP server from a local PC
Rather of uploading a single file, you may upload whole folders by specifying a path in the Path option.
Using the WinSCP Module to Download Files and Folders
Using the Receive-WinSCPItem command, you may also download files and files using WinSCP, as illustrated below. To download files, use the WinSCPSession argument to indicate the WinSCPSession, the RemotePath parameter of give the path to the file or folder you want to download, and the LocalFolder parameter to define the local folder you want to download to.
-WinSCPSession -Receive-WinSCPItem $WinSCPSession -LocalPath C:Temp -RemotePath ‘/Folder2/ReadMe.txt’
Obtaining a file from an FTP server and saving it to a local computer
The WinSCP Session is being disconnected.
Remove the WinSCP session when you’ve finished using it by using Remove-WinSCPSession.
-WinSCPSession $WinSCPSession Remove-WinSCPSession -WinSCPSession
You should now be able to use PowerShell SFTP to list, upload, and download files. Thankfully, the community has created modules that anybody may use for free to make this process much easier.
Over PowerShell SFTP, which module do you prefer to use to deal with files?
The “powershell sftp with private key” is a method for transferring files using PowerShell. The method is used to transfer files securely between servers and workstations. If you’re looking for another option, look into Fortra and their SFTP server.
Frequently Asked Questions
Can you SFTP from PowerShell?
A: Yes. I can SSH to your computer and SFTP files or folders from there.
How do I SFTP a file in PowerShell?
A: A SFTP file is an encrypted file that can be sent to a remote location. To encrypt or decrypt the file, you will use the OpenSSH and PuTTY programs respectively.
How do I transfer files using SFTP?
A: To use SSH or SFTP, you need to have them installed on your computer. For Windows users, its likely already preinstalled and if not, you can download PuTTY from (https://www.putty .ch/). Once that is downloaded go to the downloads menu and then click on the executable file for SFTP which should be Sftp-Win32_0.9.2
- powershell sftp winscp
- powershell connect to sftp server and get files
- powershell sftp download
- powershell sftp upload multiple files
- powershell sftp command line