PowerShell - Get modified time of a file on an FTP server

Posted on 12th January 2018

The following PowerShell script demonstrates how to retrieve the last modified date-time of a given file on a remote FTP server. This script uses System.Net.FtpWebRequest class to send a request to the FTP server which then respond back with the last modification time of the file. An instance of the FtpWebRequest class is created by passing a URI to the create method call. The URI is of the form:


The credentials required to authenticate the request are send to the server by setting the Credentials property of FTPWebRequest to an instance of System.Net.NetworkCredential class which is initialized with user name and password strings.

In the below script, FTP Request and response logic is wrapped inside a function named Get-FileDateTime which takes the server name, user name, password and file path as parameters and returns the DateTime value which is the last modified timestamp of the given file.

# Function to get date timestamp of a file on FTP server
Function Get-FileDateTime { 

Param (


    # Create URI by joining server name and directory path
    $uri =  "$server$FilePath" 
     # Create an instance of FtpWebRequest
    $FTPRequest = [System.Net.FtpWebRequest]::Create($uri)

    # Set the username and password credentials for authentication
    $FTPRequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)

    #Set method to GetDateTimestamp
    $FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::GetDateTimestamp

    # Enter passive mode.
    # Server waits for client to initiate a connection. 
    $FTPRequest.UsePassive = $true

    # Get response from FTP server for the request
    $FTPResponse = $FTPRequest.GetResponse() 

    # Extract last modified time from the response
    $LastModified = [DateTime]$FTPResponse.LastModified

    # Close response object
    Return $LastModified 
catch {
    #Show error message if any
    write-host -message $_.Exception.InnerException.Message

} #End of Funtion

$server = 'ftp://server_name/'
$username = 'username'
$password = 'password'
$file = 'file_path/file_name'

$dt = Get-FileDateTime -server $server -username $username -password $password -file $file
Write-host $file $dt

Sample Output

sub/myfile.txt 23/12/2016 15:42:11

Also Read

Post a comment


Nothing yet..be the first to share wisdom.