Post a reply

Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)


Topic review


Re: 'PreserveTimestamp = $false' not working -- PowerShell script

@martin: This worked.

Awesome thanks @Martin. That worked great! I missed that documentation.

[SOLVED] 'PreserveTimestamp = $false' not working -- PowerShell script

I am setting up a new job and the host is a bit more restrictive than others I've worked with. I'm continually running into the error below even though I've added the setting to not PreserveTimestamp.

Error: Exception calling "Check" with "0" argument(s): "**Upload of file 'somefile.txt' was successful, but error occurred while setting the permissions and/or timestamp.**

If the problem persists, turn off setting permissions or preserving timestamp. Alternatively you can turn on 'Ignore permission errors' option.
The server does not support the operation.
Error code: 8
Error message from server (US-ASCII): SETSTAT unsupported"

The script is basic:
    # Load WinSCP .NET assembly
    Add-Type -Path "c:\TEST\WinSCP\WinSCPnet.dll"
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = ""
        UserName = "username"
        Password = "password"
        GiveUpSecurityAndAcceptAnySshHostKey = $True
    $session = New-Object WinSCP.Session
    $session.SessionLogPath = "C:\TEST\WinSCP\Logs\winscp.log"
    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.FilePermissions = $Null # This is default
    $transferOptions.PreserveTimestamp = $false
        # Connect
        $remotePath = "./"
        $localpath = "\\networkpath\sharename\directory\"
        $files = @(
        foreach ($file in $files)
            Write-Host "Uploading $file ..."
            $session.PutFiles($localpath + $file, $remotePath, $transferOptions).Check()
        # Disconnect, clean up
    Write-Host "Error: $($_.Exception.Message)"

Oddly, it appears that the transfer option to not preserve timestamp is being ignored, according to the log:
> DATE TIME Script: put -delete -nopermissions -preservetime -transfer="binary" -- "\\networkpath\sharename\directory\somefile.txt" "./"

I can provide additional log detail if necessary.

I have also tried using these transfer options:
$transferOptions.AddRawSettings("IgnorePermErrors", "1")
$transferOptions.AddRawSettings("PreserveTimeDirs", "0")

WinSCP Version 5.17.8 (Build 10803) (OS 10.0.17763 - Windows Server 2019 Standard)