Add Files Over FTP With PowerShell

Add Files Over FTP With PowerShell

Powershell logo

The File Transfer Protocol (FTP) is a typical carrier worn to switch files between possibilities and servers. It’s continually precious to automate these file transfers, and PowerShell scripts can attain in to hand to run up this assignment.

How To Inform FTP in PowerShell

There are about a numerous techniques to assemble FTP transfers in PowerShell.

The splendid is to spend WebClient.UploadFile. PowerShell is an object-oriented scripting language, and you’ve got got full web admission to to .NET long-established libraries with Original-Object. With this, you might perchance perchance also manufacture a brand fresh WebClient, area the credentials for it, and upload a file.

$consumer = Original-Object Machine.Secure.WebClient
$consumer.Credentials = Original-Object Machine.Secure.NetworkCredential("username", "password")
$consumer.UploadFile("", "")

This also can work gorgeous, nevertheless won’t have the chance to address TLS/SSL encrypted requests, or assemble “active” FTP transfers. Using FtpWebRequest, lined below, will clear up this venture.

It isn’t most productive discover to retailer your username and password in a script nonetheless, in particular if that script is being committed to a shared Git repository. You might have the chance to as an different area ambiance variables like FtpUsername and web admission to them in the script.

feature uploadToFTPServer($far-off, $native) {
    $consumer = Original-Object Machine.Secure.WebClient
    $consumer.Credentials = Original-Object Machine.Secure.NetworkCredential($Env:FtpUsername, $Env:FtpPassword)
    $consumer.UploadFile($far-off, $native)

uploadToFTPServer "", ""

Making this actual into a feature will furthermore can allow you to easily assemble a few transfers by calling the feature with numerous parameters.

Improved FTP Transfers with PowerShell

Within the occasion you are going to love extra protect watch over, you might perchance perchance also mute spend FtpWebRequest. This also can strengthen TLS transfers, and furthermore aid you to flip passive mode off. The very best technique to spend it’s far to commence a file stream and reproduction it to the FTP stream.

feature uploadToFTPServer($far-off, $native) {
    $request = [System.Net.FtpWebRequest]::Model($far-off)
    $request.Credentials = Original-Object Machine.Secure.NetworkCredential($Env:FtpUsername, $Env:FtpPassword)
    $request.Contrivance = [System.Net.WebRequestMethods+Ftp]::UploadFile
    $request.UsePassive = $ethical

    $fileStream = [System.IO.File]::OpenRead($native)
    $ftpStream = $request.GetRequestStream()



uploadToFTPServer "" ""

Because it’s the usage of file streams, and never reading all of the bytes, this has the splendid thing about working greater with big file transfers.

Sending SFTP Transfers with Posh-SSH

SFTP is an alternate FTP protocol that operates over SSH. It’s fairly extra complicated to spend than smartly-liked FTP, since you might perchance perchance also’t objective ship a username and password, and it’s now not supported by native PowerShell.

You’ll want to put in the Posh-SSH equipment to focus on over SFTP:

Install-Module -Name Posh-SSH

Then you definately’ll have the chance to launch a brand fresh session, the usage of a brand fresh credential object. This works in the same draw as web request transfers, except you’ll furthermore want to shut the session on the cease.

Import-Module Posh-SSH

$Password = ConvertTo-SecureString $Env:FtpPassword -AsPlainText -Force
$Credential = Original-Object Machine.Management.Automation.PSCredential ($Env:FtpUsername, $Password)

$FilePath = ""
$SftpPath = '/folder'

$ThisSession = Original-SFTPSession -ComputerName '' -Credential $Credential

Position-SFTPFile -SessionId ($ThisSession).SessionId -LocalFile $FilePath -RemotePath $SftpPath
Accept-SFTPSession | % { Lift away-SFTPSession -SessionId ($_.SessionId) }

Sending Mammoth Files with a Growth Bar in PowerShell

Using the file stream’s CopyTo is discreet, nevertheless for long transfers, you might perchance perchance desire some form of growth monitoring. Here’s fairly complicated to add, since you’ll want to reproduction the streams your self, nevertheless works smartly with the following script:

$request = [Net.WebRequest]::Model("")
$request.Credentials =
Original-Object Machine.Secure.NetworkCredential($Env:FtpUsername, $Env:FtpPassword)
$request.Contrivance = [System.Net.WebRequestMethods+Ftp]::UploadFile

$fileStream = [System.IO.File]::OpenRead("")
$ftpStream = $request.GetRequestStream()

$buffer = Original-Object Byte[] 10240
while (($learn = $fileStream.Read($buffer, 0, $buffer.Length)) -gt 0)
    $ftpStream.Write($buffer, 0, $learn)
    $pct = ($fileStream.Location / $fileStream.Length)
    Write-Growth `
        -Inform "Importing" -Situation ("{0:P0} whole:" -f $pct) `
        -PercentComplete ($pct 100)