A friend asked me if Powershell can do simple file up and downloads. My answer was, yes of course, very easy. So this is a post with a little information about how you can do a FTP Upload or a FTP Download using Powershell. To be clear, of course you can do much more with PowerShell and FTP. But this post should show you the basic function of FTP transfers in Powershell based on the .NET Framework. With that here is how PowerShell FTP works:

## FTP Upload file using PowerShell

This is how you can upload a file using FTP with PowerShell.

```# Config
\$LocalFile = "C:\Temp\file.zip"

# Create FTP Rquest Object
\$FTPRequest = [System.Net.FtpWebRequest]::Create("\$RemoteFile")
\$FTPRequest = [System.Net.FtpWebRequest]\$FTPRequest
\$FTPRequest.UseBinary = \$true
\$FTPRequest.UsePassive = \$true
\$FileContent = gc -en byte \$LocalFile
\$FTPRequest.ContentLength = \$FileContent.Length
# Get Stream Request by bytes
\$Run = \$FTPRequest.GetRequestStream()
\$Run.Write(\$FileContent, 0, \$FileContent.Length)
# Cleanup
\$Run.Close()
\$Run.Dispose()```

And this is how you can download files from an FTP server using PowerShell.

```# Config
\$LocalFile = "C:\Temp\file.zip"

# Create a FTPWebRequest
\$FTPRequest = [System.Net.FtpWebRequest]::Create(\$RemoteFile)
\$FTPRequest.UseBinary = \$true
\$FTPRequest.KeepAlive = \$false
# Send the ftp request
\$FTPResponse = \$FTPRequest.GetResponse()
\$ResponseStream = \$FTPResponse.GetResponseStream()
# Create the target file on the local system and the download buffer
\$LocalFileFile = New-Object IO.FileStream (\$LocalFile,[IO.FileMode]::Create)
do {
}

So let me know how FTP file transfers with PowerShell are working for you. Scenarios I have used this are simple automation tasks where we needed to upload or download some files.

Tags: , , , , , , , , , , , , , , , , Last modified: August 18, 2018