---
title: "Activate PowerShell SFTP to Transfer Files"
description: "Learn step-by-step tutorial how to work with files remotely via SFTP and dig into some of the different PowerShell SFTP modules you can use. Learn how to list, upload and download files via PowerShell SFTP."
canonical: "https://adamtheautomator.com/powershell-sftp/"
---

# Activate PowerShell SFTP to Transfer Files

> Learn step-by-step tutorial how to work with files remotely via SFTP and dig into some of the different PowerShell SFTP modules you can use. Learn how to list, upload and download files via PowerShell SFTP.

Source: https://adamtheautomator.com/powershell-sftp/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Activate PowerShell SFTP to Transfer Files](https://adamtheautomator.com/wp-content/uploads/2021/09/How-to-Transfer-files-with-PowerShell-and-SFTP-Step-by-Step.jpg)

# Activate PowerShell SFTP to Transfer Files

[![](https://secure.gravatar.com/avatar/c14b031fe4a12b0e3f4d1e112e37261df83921ddc370cdafbad2a614e36f0cf4?s=192&d=mm&r=g)Chaitanya](https://adamtheautomator.com/author/chaitanya-g/)30 September 20215 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[File Management](/tag/file-management/)[Powershell Administration](/tag/powershell-administration/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Managing Files over PowerShell SFTP with the Posh-SSH Module](#managing-files-over-powershell-sftp-with-the-posh-ssh-module)
*   [Downloading Files and Folders from the SSH server](#downloading-files-and-folders-from-the-ssh-server)
*   [Uploading Files and Folders to the SSH server](#uploading-files-and-folders-to-the-ssh-server)
*   [Disconnecting the Posh-SSH Session](#disconnecting-the-posh-ssh-session)
*   [Managing Files over SFTP with the WinSCP Module](#managing-files-over-sftp-with-the-winscp-module)
*   [Inspecting Files and Folders with the WinSCP Module](#inspecting-files-and-folders-with-the-winscp-module)
*   [Uploading Files and Folders with the WinSCP Module](#uploading-files-and-folders-with-the-winscp-module)
*   [Downloading Files and Folders with the WinSCP Module](#downloading-files-and-folders-with-the-winscp-module)
*   [Disconnecting the WinSCP Session](#disconnecting-the-winscp-session)
*   [Conclusion](#conclusion)

If you need to transfer files securely with SFTP and know a little about PowerShell, you’re in luck. In this tutorial, you will learn how to work with files remotely via SFTP and dig into some of the different PowerShell SFTP modules you can use.

Not a reader? Watch this related video.

**_Not seeing the video? Make sure your ad blocker is disabled._**

Let’s dive in!

## Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following:

*   A remote SFTP server – This tutorial will use Windows Server 2019’s FTP server.
*   PowerShell – You can use both Windows PowerShell 5.1 or PowerShell [v7](https://adamtheautomator.com/powershell-7-upgrade/)+.

Related:[How to Walk Through a PowerShell 7 Upgrade](https://adamtheautomator.com/powershell-7-upgrade/)

## Managing Files over PowerShell SFTP with the Posh-SSH Module

The Posh-SSH module is a handy module to work with files over SFTP. To get started, open PowerShell as administrator and run `Install-Module` to download and install the module from the PowerShell Gallery. If prompted about an untrusted repository, type Y or A to confirm.

```powershell
Install-Module -Name Posh-SSH
```

### Downloading Files and Folders from the SSH server

Now that you have installed the module let’s look at downloading files. To download files, you must first set up a session. The session is a persistent connection you’ll use to perform various SFTP operations.

To set up a session, provide a PSCredential object via `Get-Credential` and specify the remote SFTP server you’d like to connect to using the `New-SFTPSession` command. When invoked via the command below, PowerShell will prompt you for a username and password.

```powershell
$SFTPSession = New-SFTPSession -ComputerName 10.0.0.10 -Credential (Get-Credential)
```

![New-SFTPSession Credential Prompt](https://adamtheautomator.com/wp-content/uploads/2021/09/image-256.png)

New-SFTPSession Credential Prompt

If the connection is successful, the command will prompt you to accept the **[Server SSH Fingerprint](https://superuser.com/questions/421997/what-is-a-ssh-key-fingerprint-and-how-is-it-generated)**. This certificate fingerprint will be how the server trusts the connection from your client.

![New-SFTPSession cmdlet output ](https://adamtheautomator.com/wp-content/uploads/2021/09/image-257.png)

New-SFTPSession cmdlet output

Now that you’ve connected to the remote server run the below command to download a file from the remote server. This `Get-SFTPItem` command downloads the **/home/user01/Downloads/test.txt** file on the remote server to the C:\\temp location on the local computer. You’ll also see that the command below uses the `SessionId` property on the `SFTPSession` object captured in the last step.

```powershell
Get-SFTPItem -SessionId $SFTPSession.SessionId -Path /home/user01/Downloads/test.txt -Destination c:\temp
```

> _You can also download entire folders by providing a path such as `/home/user01/Downloads` to the `Path` parameter._

### Uploading Files and Folders to the SSH server

Now that you’ve seen how to download files and folders from your SSH server let’s look at how to upload files and folders. The method to upload files is nearly identical to downloading files, as you can see below.

Invoke the `Set-SFTPItem` command providing the session to connect to, the file to upload, and the location on the SSH server to upload the file to.

```powershell
Set-SFTPItem -SessionId $SFTPSession.SessionId -Path C:\Temp\test.txt -Destination /tmp
```

> _You can also upload entire folders by providing a folder path such as `C:\Temp\TestFolder` to the `Path` parameter._

### Disconnecting the Posh-SSH Session

Finally, once you are done using the Posh-SSH session, disconnect and remove it from memory with the `Remove-SFTPSession`. Although this step isn’t mandatory, doing so will cleanly disconnect the server’s connection and free up memory in PowerShell.

```powershell
Remove-SFTPSession -SFTPSession $SFTPSession
```

## Managing Files over SFTP with the WinSCP Module

If you’d prefer not to use the Posh-SSH module for whatever reason, you always have another option; the WinSCP module! The WinSCP module is another handy module that comes from the popular [WinSCP GUI utility](https://winscp.net/eng/index.php).

To get started, like the Posh-SSH module, first download and install it from the PowerShell Gallery, as shown below.

```powershell
Install-Module -Name WinSCP 
```

Related:[The WinSCP Command-Line: Ultimate Guide](https://adamtheautomator.com/winscp-command-line/)

### Inspecting Files and Folders with the WinSCP Module

Now that you’ve installed the module, you can get started using it. Let’s first make a connection to the server. Like the Posh-SSH module, you must first always establish a connection to the remote server.

The first time you attempt to establish a connection to the SFTP server, the `New-WinSCPSession` command will prompt you to add the server’s public key to your local certificate repository. You must do this so that the SFTP server will trust your local computer.

Click the **Copy key fingerprints to clipboard** and paste the fingerprint into a text window to add your key. You’ll need this in the next step. Next, copy the line that looks like “ssh-ed25519 255 48:b4:4e:3d:67:ca:25:e4:c7:77:fe:3c:df:ae:d9:b9” anCopy and paste that fingerprint in the script as shown in below script.

![Inspecting Files and Folders with the WinSCP Module](https://adamtheautomator.com/wp-content/uploads/2021/09/image-258.png)

nspecting Files and Folders with the WinSCP Module

To establish a connection, below you’ll see an example script. This script creates a PSCredential object that is passed to the `Credential` parameter of the `New-WinSCPSessionOption` command and the remote server to connect to and the protocol `sftp`.

```powershell
#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")
```

Now that you’ve established a connection to the server run the `Get-WinSCPChildItem` command and provide the session you just created. This command will enumerate all the files and folders on the remote server from the remote server user’s home directory.

```powershell
Get-WinSCPChildItem -WinSCPSession $WinSCPSession
```

![Displaying FTP server's content](https://adamtheautomator.com/wp-content/uploads/2021/09/image-259.png)

Displaying FTP server’s content

To only list files in a particular directory, specify the `Path` parameter as shown below.

```powershell
Get-WinSCPChildItem -WinSCPSession $WinSCPSession -Path '/Folder2'
```

![Displaying FTP server's content in a subfolder](https://adamtheautomator.com/wp-content/uploads/2021/09/image-260.png)

Displaying FTP server’s content in a subfolder

### Uploading Files and Folders with the WinSCP Module

Uploading files and folders is similar to listing files with the WinSCP module. To do so, run the `Send-WinSCPItem` command as shown below. By specifying the session (`WinSCPSession`), the local `Path` of the file to upload, and the `RemotePath` to where the file should go, `Send-WinSCPItem` will upload the file.

By default, if you do not specify a path using `-RemotePath` parameter, your files and folders will be uploaded to the user’s home directory you authenticated to the server with.

```powershell
Send-WinSCPItem -WinSCPSession $WinSCPSession -Path C:\Temp\TestFile.txt -RemotePath '/Folder2/'
```

When the upload completes, you’ll see that `Send-WinSCPItem` displays the remote folder path containing the file uploaded.

![Uploading a file from local PC to FTP server](https://adamtheautomator.com/wp-content/uploads/2021/09/image-261.png)

Uploading a file from local PC to FTP server

> _You can upload entire folders by providing a path to the `Path` parameter rather than a single file._

### Downloading Files and Folders with the WinSCP Module

Downloading files and files with the WinSCP is possible, too, using the `Receive-WinSCPItem` command, as shown below. To download files, specify the `WinSCPSession` again, provide the path to the file or folder you’d like to download via the `RemotePath` parameter and the local folder you’d like to download to.

```powershell
Receive-WinSCPItem -WinSCPSession $WinSCPSession -RemotePath '/Folder2/ReadMe.txt' -LocalPath C:\Temp
```

![Downloading file from FTP server to local PC](https://adamtheautomator.com/wp-content/uploads/2021/09/image-262.png)

Downloading file from FTP server to local PC

### Disconnecting the WinSCP Session

Once you are done using the WinSCP session, disconnect and remove it using the `Remove-WinSCPSession`.

```powershell
Remove-WinSCPSession -WinSCPSession $WinSCPSession
```

## Conclusion

You should now know how to list, upload and download files via PowerShell SFTP. Thankfully, the community has made modules for everyone to use freely to make this job much more manageable.

Which module do you prefer to work with files over PowerShell SFTP?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-sftp%2F&text=Activate%20PowerShell%20SFTP%20to%20Transfer%20Files)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-sftp%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-sftp%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/11/How-to-Install-NitroShare-for-Cross-Platform-File-Transfer.jpg)

### [Install NitroShare and Streamline Your File Transfers Today](/nitroshare/)

Simplify cross-platform file transfers with NitroShare. This tutorial guides you through installation and use for fast, effortless file sharing between different operating systems.

![](https://adamtheautomator.com/wp-content/uploads/2021/06/winSCP-command-line.jpg)

### [WinSCP Command-Line Simplified : Your Complete Reference](/winscp-command-line/)

Explore the depths of WinSCP Command-Line in our guide. Master file transfer and automation with expert tips, techniques, and practical examples for users at all levels.

![](https://adamtheautomator.com/wp-content/uploads/2023/03/seafile.jpg)

### [Get Started With Seafile Self-Hosted Cloud Storage](/seafile/)

Get started with your own cloud storage solution. Seafile offers a self-hosted alternative with easy setup, secure file sharing, and data privacy.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
