Now that you know how to connect to an FTP server let's check how you can upload some files to the FTP server. As you can see below, I'm first connecting to the FTP server, then listing current files and checking the content, then checking the directory's content within the FTP server. Finally, I get a list of files stored locally and upload them one by one into the FTP server by using the Send-FTPFile cmdlet. You can either use this command to send file by file, including auto-creation of the remote directory or upload all files at once.
$Client = Connect-FTP -Server '192.168.241.187' -Verbose -Username 'test' -Password 'BiPassword90A' -EncryptionMode Explicit -ValidateAnyCertificate
# List files
$List = Get-FTPList -Client $Client
$List | Format-Table
# List files within Temporary directory
$List = Get-FTPList -Client $Client -Path '/Temporary'
$List | Format-Table
# Get local files
$ListFiles = Get-ChildItem -LiteralPath $PSScriptRoot\Upload
# Upload file by file
foreach ($File in $ListFiles) {
# To temporary
Send-FTPFile -Client $Client -LocalPath $File.FullName -RemotePath "/Temporary/$($File.Name)" -RemoteExists Overwrite
# to directory within Temporary that may not exists
Send-FTPFile -Client $Client -LocalPath $File.FullName -RemotePath "/Temporary/CreateDir/$($File.Name)" -RemoteExists Skip -CreateRemoteDirectory
}
# Upload all files at once to FTP
Send-FTPFile -Client $Client -LocalPath $ListFiles.FullName -RemotePath "/Temporary" -RemoteExists Overwrite
Disconnect-FTP -Client $Client
Similarly, if you want to download files from an FTP server, you would connect to FTP, get a list of files on FTP, find the files you need, and download those files – all of that in less than 10 lines of code.
# Connect to FTP
$Client = Connect-FTP -Server 'test.rebex.net' -Verbose -Username 'demo' -Password 'password'
# Get list of files on FTP
$List = Get-FTPList -Client $Client -Path '/pub/example'
# Find latest file on FTP server
$FindLatestFile = $List | Where-Object { $_.Type -eq 'File' } | Sort-Object -Property Modified -Descending | Select-Object -First 2
# Download that file
foreach ($RemoteFile in $FindLatestFile) {
Receive-FTPFile -Client $Client -RemoteFile $RemoteFile -LocalPath "$PSScriptRoot\Download\$($RemoteFile.Name)" -LocalExists Overwrite -VerifyOptions Retry, None
}
# Download multiple files into directory
Receive-FTPFile -Client $Client -RemoteFile $FindLatestFile -LocalPath "$PSScriptRoot\Download" -LocalExists Overwrite -VerifyOptions Retry, None
# Disconnect
Disconnect-FTP -Client $Client
Of course, uploading and downloading files from an FTP server using PowerShell is not the only thing you can do on an FTP server. You may want to get or set permissions, or for example, if something is not working correctly, you may want to see what's happening behind the scenes and look at what the FTP server is reporting.
# If you want to track responses from FTP
Set-FTPTracing -Enable -DisplayConsole
$Client = Connect-FTP -Server '192.168.241.187' -Verbose -Username 'test' -Password 'BiPassword90A' -EncryptionMode Explicit -ValidateAnyCertificate
# List files
Get-FTPList -Client $Client -Path '/Temporary' | Format-Table *
Get-FTPChmod -Client $Client -RemotePath '/Temporary'
# Set / Read Chmod - you need to have permissions for this to work properly
Set-FTPChmod -Client $Client -RemotePath '/Temporary/OrgChart (1).pdf' -Permissions 666
Set-FTPChmod -Client $Client -RemotePath '/Temporary/CreateDir' -Permissions 666
# Set / Read Chmod - you need to have permissions for this to work properly
Get-FTPChmod -Client $Client -RemotePath '/Temporary/OrgChart (1).pdf'
Get-FTPChmod -Client $Client -RemotePath '/Temporary/CreateDir'
Set-FTPTracing -Disable
Using Set-FTPTracing to enable or disable visibility of commands being sent to the FTP server can be very useful to track what is actually happening behind the scenes. If you want to get current permissions of a file or folder, you will use Get-FTPChmod to set permissions Set-FTPChmod. Of course – to change something, you may need to have permissions first. Finally, if you would like to test if the file exists on the source, you can do this using the Test-FTPFile command.