Check file existence on FTP server in PowerShell


I want to check if some file exists on FTP server. I wrote code with Test-Path but it’s not working. Then I wrote code to get FTP server file size, but it’s not working either.

My code

This code is not working.


Get-Content : Cannot find drive. A drive with the name ‘ftp’ does not exist.At C:\documents\upload-file.ps1:67 char:19
+ $sourcefilesize = Get-Item($urlDest)
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (ftp:String) [Get-Content], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Any idea how to solve this error? Is there any way I can check some exists into FTP server? Any clue regarding this will be helpful.


You cannot use Test-Path nor Get-Content with FTP URL.

You have to use FTP client, like WebRequest (FtpWebRequest).

Though it does not have any explicit method to check file existence (partly because FTP protocol itself does not have such functionality). You need to “abuse” a request like GetFileSize or GetDateTimestamp.

The code is based on C# code from How to check if file exists on FTP before FtpWebRequest.

If you want a more straightforward code, use some 3rd party FTP library.

For example with WinSCP .NET assembly, you can use its Session.FileExists method:

(I’m the author of WinSCP)


Check file existence on FTP server in PowerShell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply