free counter
Science And Nature

How exactly to Upload Files Over FTP With PowerShell

Powershell logo

The File Transfer Protocol (FTP) is really a common service used to transfer files between clients and servers. Its often beneficial to automate these file transfers, and PowerShell scripts will come in handy to increase this technique.

HOW EXACTLY TO Use FTP in PowerShell

There are some different ways to create FTP transfers in PowerShell.

The easiest is by using WebClient.UploadFile. PowerShell can be an object-oriented scripting language, and you also have full usage of .NETstandard libraries with New-Object. With this particular, you can develop a new WebClient, set the credentials for this, and upload a file.

$client = New-Object System.Net.WebClient
$client.Credentials = New-Object System.Net.NetworkCredential("username", "password")
$client.UploadFile("ftp://example.com/path/archive.zip", "C:archive.zip")

This can work fine, but wont have the ability to handle TLS/SSL encrypted requests, or make active FTP transfers. Using FtpWebRequest, covered below, will solve this issue.

It isnt best practice to store your account in a script however, particularly if that script has been focused on a shared Git repository. It is possible to instead set environment variables likeFtpUsernameand access them in the script.

function uploadToFTPServer($remote, $local) 
    $client = New-Object System.Net.WebClient
    $client.Credentials = New-Object System.Net.NetworkCredential($Env:FtpUsername, $Env:FtpPassword)
    $client.UploadFile($remote, $local)


uploadToFTPServer "ftp://example.com/path/archive.zip", "C:archive.zip"

Causeing this to be right into a function may also enable you to easily make multiple transfers by calling the event with different parameters.

Advanced FTP Transfers with PowerShell

If you want more control, you need to use FtpWebRequest. This can support TLS transfers, and in addition permit you to turn passive mode off. The simplest way to use it really is to open a file stream and copy it to the FTP stream.

function uploadToFTPServer($remote, $local) 
    $request = [System.Net.FtpWebRequest]::Create($remote)
    $request.Credentials = New-Object System.Net.NetworkCredential($Env:FtpUsername, $Env:FtpPassword)
    $request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
    $request.UsePassive = $true

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

    $fileStream.CopyTo($ftpStream)

    $ftpStream.Dispose()
    $fileStream.Dispose()


uploadToFTPServer "ftp://example.com/archive.zip" "C:archive.zip"

Since its using file streams, rather than reading all of the bytes, it has the advantage of working better with huge file transfers.

Sending SFTP Transfers with Posh-SSH

SFTP can be an alternate FTP protocol that operates over SSH. Its a little more complicated to utilize than regular FTP, because you cant just send a account, and its own not supported by native PowerShell.

Youll have to install the Posh-SSHpackage to communicate over SFTP:

Install-Module -Name Posh-SSH

Then youll have the ability to take up a new session, utilizing a new credential object. This works just as as web request transfers, except youll should also close the session by the end.

Import-Module Posh-SSH

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

$FilePath = "C:archive.zip"
$SftpPath = '/folder'

$ThisSession = New-SFTPSession -ComputerName '1.2.3.4' -Credential $Credential

Set-SFTPFile -SessionId ($ThisSession).SessionId -LocalFile $FilePath -RemotePath $SftpPath
Get-SFTPSession | %  Remove-SFTPSession -SessionId ($_.SessionId) 

Sending Huge Files with a Progress Bar in PowerShell

Utilizing the file streams CopyTois easy, but also for long transfers, you might want some type of progress monitoring. It is a little complicated to include, since youll need to copy the streams yourself, but is effective with the next script:

$request = [Net.WebRequest]::Create("ftp://example.com/path/archive.zip")
$request.Credentials =
New-Object System.Net.NetworkCredential($Env:FtpUsername, $Env:FtpPassword)
$request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile

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

$buffer = New-Object Byte[] 10240
while (($read = $fileStream.Read($buffer, 0, $buffer.Length)) -gt 0)

    $ftpStream.Write($buffer, 0, $read)
    $pct = ($fileStream.Position / $fileStream.Length)
    Write-Progress `
        -Activity "Uploading" -Status (":P0 complete:" -f $pct) `
        -PercentComplete ($pct 100)


$ftpStream.Dispose()
$fileStream.Dispose()

Read More

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker