Problem solve Get help with specific problems with your technologies, process and projects.

Use a PowerShell FTP script to upload and download files

By using the .NET WebClient class and PowerShell, Windows admins can upload and download files to FTP.

Sometimes a system administrator needs to get files from point A to point B. And there are many ways to accomplish...

this, but one popular method is to use a PowerShell FTP script. Admins can do this by using Windows' .NET capabilities.

Using PowerShell, Windows admins can use the .NET WebClient class. Let's create a functional PowerShell FTP script from scratch that can download and upload files. This script will have five essential tasks:

  1. Getting the input
  2. Creating the WebClient object
  3. Creating the Credential object
  4. Creating the Uniform Resource Identifier
  5. Downloading/Uploading the file

The first task is to gather the input from the user. For input data, we’ll include everything needed to use a FTP server through the graphical user interface. This includes username, password, file names and the FTP server name. I’ll assign these as variables so I can easily change them later if needed.

$UserName = 'myusername'

$Password = 'mypassword'

$LocalFilePath = 'C:\myfile.txt'

$RemoteFileName = 'myfile.txt'

$ServerName = 'myftpserver.somedomain.com'

I’ve assigned all of the input data I need into variables. Next, we'll instantiate a WebClient object.

$webclient = New-Object System.Net.WebClient

This command gives us the object to work with that we can use later to upload or download the file.

The WebClient object has a Credentials property, which PowerShell uses to pass the username and password to the FTP server. This Credentials property needs a System.Net.NetworkCredential object that needs two parameters: UserName and Password. We’ll use the username and password we assigned earlier, as such:

$webclient.Credentials = New-Object System.Net.NetworkCredential($UserName, $Password)

Decide where to download or upload a file

We now have a WebClient object with the Credentials property populated and must decide where to download or upload a file. Depending on what we choose, the uniform resource identifier (URI) will be different.

For example, if I want to download a file from a remote FTP server, the URI needs to be in the form of ftp://SERVERNAME/filename.ext. We’ll build a System.Uri object, which has a parameter of the actual URI. Then we'll use the $RemoteFileName variable to specify which folder in the FTP server’s root to download.

$uri = New-Object System.Uri(“ftp://$ServerName/$RemoteFileName”)

We’ve gone through all of the prerequisites for downloading a file with FTP. Now let’s actually get a file downloaded. To download a file on a remote FTP server, use the DownloadFile method. The DownloadFile method has two parameters that you’ll need to send to it -- the URI and the path to which the file needs to be downloaded.

$webclient.DownloadFile($uri, $LocalFilePath)

This single line connects to the FTP server with the username and password specified earlier, looks for the file myfile.txt in the root of the server and then transfers that file to the root of C:

To upload a file, use the UploadFile() method. The command to do this is exactly the same as the one to download the file, but admins should just replace Download with Upload.

$webclient.UploadFile($uri, $LocalFilePath)

This will upload the C:\myfile.txt to the root of the FTP server.
 

Putting it into a single PowerShell script

Now let's take everything we covered above and wrap it up in a single script called Move-FtpFile.ps1.

param($Username,$Password,$FilePath,$ServerName,$Action)

 

$webclient = New-Object System.Net.WebClient

 

$webclient.Credentials = New-Object System.Net.NetworkCredential($UserName, $Password)

 

$file = Get-Item -Path $FilePath

$uri = New-Object System.Uri(“ftp://$ServerName/$($file.Name)”)

if ($Action -eq ‘Download’) {

$webclient.DownloadFile($uri, $FilePath)

} elseif ($Action -eq ‘Upload’) {

$webclient.UploadFile($uri, $FilePath)

}

The only change I made was parameterizing the input and adding the $Action parameter. I added the $Action parameter to give you an easy way to either download or upload the file.  The Move-FtpFile.ps1 file can then be called this:

PS> .\Move-FtpFile.ps1 -Username ‘myuser’ -Password ‘mypassword’ -FilePath ‘C:\myfile.txt’ -ServerName ‘myftpserver.somedomain.com’ -Action Download

Next Steps

Use PowerShell variables to prevent accidental changes to a script

Understand common automatic variables in PowerShell

This was last published in August 2015

Dig Deeper on Windows File Management

PRO+

Content

Find more PRO+ content and other member only offers, here.

Join the conversation

5 comments

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

Have you used FTP with PowerShell to move files?
Cancel
Should use sftp with a .ppk cert when possible. Transmitting over ftp is unnecessarily risky.
Cancel
It's ridiculous, isn't it, that after all these decades we don't have something more intuitive and user friendly than this?
Cancel
Hi Peter,

Yes. I agree. There are more secure ways to transfer files. I wouldn't suggest doing something like this over the Internet unless the data being transmitted isn't too important or sensitive.

I've built other PowerShell scripts that use SFTP. Perhaps I should do an article on that in the future.
Cancel
Hi Sharon,

There are plenty of other ways to transfer files via FTP. In this article, I'm just showing you the nuts and bolts of how it can be doing with .NET and PowerShell. One of the nice things about PowerShell is that you can wrap up all this code into a function which you can then do something like Download-File or Upload-File. It takes all the messiness out of it. :)
Cancel

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchWinIT

SearchEnterpriseDesktop

SearchVirtualDesktop

Close