Download and delete files from an FTP server, before logging activity locally

I was wondering if someone could help me to create PowerShell script please.

The PowerShell script will run daily as a scheuled task and will need to do the following.

  1. Connect to an external FTP server. Its worth mentioning that I don’t want credentials to be saved in the script itself as readable strings. Having looked in to this, it seems that this can be acheived by providing credentials which can then be saved to an XML file and encrypted

Get-Credential | Export-Clixml -Path

This will encrypt the username and password provided and place the credenitals in to an XML file. This encrypted file content can then be put in to a variable in the main script which can then be referenced without visually presenting the strings.

  1. Download files from a given directory on the FTP server to a local directory and renamed. The file names that will need to be targeted on the FTP server will adhere to a set naming convention that increments sequentially but be prefixed ‘L6XQ’.

  2. The script will then need to check that the files have arrived in the local directory. Once it’s confirmed that they have, the same files will need to be deleted from the FTP server.

  3. I would like the above processs to be recorded in to a text log file with timestamps if possible.

  4. A bonus would be some logic included in the script which could trigger an email to chosen users, listing the file or files that have been obtained.

Can anyone advise or suggest some relevant articles/resources?


Hi Andrew, what did you tried yourself until now? Can you show us the code that you produced till now?

Remark about the credential piece: yes you can do it, but only the user account that has generated the xml file with the credentials, is also the account that can retrieve those same credentials.

If I recall, its also only on the server where the file is generated? I think I read somewhere that both the user and server is part of the encryption.

Hi Richard

I understand that when I use the Get-Credential | Export-Clixml -Path command to write credentials to the XML file that this will be tied to the user account which created the credentials. This is fine as the server that will be accesing the FTP site is constantly logged in and the account will be the same used to create the credentials.

In terms of what I’ve tried already, nothing in practice as of yet, although Im setting up a WS2012R2 VM at home as a test box and will try out a few of the following methods later to see which suites best.

I’ve not been able to find anything suitable yet for verifying that files have arrived, then deleting the files on the FTP seerver and logging, triggering a notification email

#Reference the credentials created as mentioned before
$MyCredential = Import-Clixml -Path "C:\Cred\PW.txt"
$LocalDirectory = "C:\Parcel_Force_FTP\Received\"
$PFFTP = "ftp//*.*"
$WebClient = New-Object System.Net.WebClient
$URI = New-Object System.Uri ($PFFTP)
#Download the file to the Temp directory for naming
$WebClient.DownloadFile ($URI,  $LocalDirectory)
#Move the file to the Receive directory and change its name
Move-Item "C:\Parcel_Force_FTP\Received\Temp\FileName (C:\Parcel_Force_FTP\Received\NewName{0:yyyMMdd}.txt" -f (Get-Date))

Second method I looked at.

 $MyCredential = Import-Clixml -Path 'C:\Cred\PW.txt'
 $Source = 'FTP://*.*'
 $Destination = 'C:/Cred/Test1.txt'
 Invoke-WebRequest -URI  $Source -OutFile  $Destination -Credential $MyCredential

I’m pretty new to PS so any guidance you could offer would be much appreciated.



I found a PowerShell FTP Client module via Google which you might want to test. No need to reinvent the wheel if it works.

Hi Daniel

I downloaded this module a couple of week ago but can;t seem to get my head around it. The help files are quite minimal.

so far i have this

$MyCredential = Import-Clixml -Path 'C:\Cred\PW.txt'
$Session = 'NatWestFTPSession'
$Source = 'FTP://'
$SourcePath = '/NatWest/L6XQ*'
$Destination = 'C:/Cred/'
$CheckFile = "C:\AdvWorks\MovieItemLog.txt"
$FileExists = Test-Path $CheckFile
Invoke-WebRequest -URI  $Source -OutFile  $Destination -Credential $MyCredential
IF ($FileExists -eq $True) {}

This gets the files I need and puts them in the local directory. I now just need something for the ‘IF’ block to say that if files are in the local directory and match a naming pattern, go out to the directory on the FTP server and delete the files.

For instance the equivalent of

IF ($FileExists -eq $True) {Remove-item -path $SourcePath -include L6XQ* -Force}

But against an FTP server location