Delete files on upload the files
[Reflection.Assembly]::LoadFrom("C:\Program Files\WinSCP\WinSCPnet.dll") | Out-Null # Session.FileTransferred event handler function FileTransferred { Param($e) if ($e.Error -eq $Null) { Write-Host ("Upload of {0} succeeded" -f $e.FileName) #If Download succeeded, remove the files from source } else { Write-Host ("Upload of {0} failed: {1}" -f $e.FileName, $e.Error) } if ($e.Chmod -ne $Null) { if ($e.Chmod.Error -eq $Null) { Write-Host ("Permisions of {0} set to {1}" -f $e.Chmod.FileName, $e.Chmod.FilePermissions) } else { Write-Host ("Setting permissions of {0} failed: {1}" -f $e.Chmod.FileName, $e.Chmod.Error) } } else { Write-Host ("Permissions of {0} kept with their defaults" -f $e.Destination) } if ($e.Touch -ne $Null) { if ($e.Touch.Error -eq $Null) { Write-Host ("Timestamp of {0} set to {1}" -f $e.Touch.FileName, $e.Touch.LastWriteTime) } else { Write-Host ("Setting timestamp of {0} failed: {1}" -f $e.Touch.FileName, $e.Touch.Error) } } else { # This should never happen with Session.SynchronizeDirectories Write-Host ("Timestamp of {0} kept with its default (current time)" -f $e.Destination) } foreach ($e in $synchronizationResult.uploads) { $removalResult = $session.RemoveFiles($session.EscapeFileMask($e.FileName)) if ($removalResult.IsSuccess) { Write-Host ("Removing of file {0} succeeded" -f $e.FileName) } else { Write-Host ("Removing of file {0} failed" -f $e.FileName) }} } # Main script try { $sessionOptions = New-Object WinSCP.SessionOptions $sessionOptions.Protocol = [WinSCP.Protocol]::sftp $sessionOptions.PortNumber = 9922 $sessionOptions.Timeout = "120" $sessionOptions.HostName = "123456" $sessionOptions.UserName = "123456" $sessionOptions.Password = "123456" $sessionOptions.SshHostKeyFingerprint = "123456" $session = New-Object WinSCP.Session try { $localPath = "C:\Users\rahimm\Desktop\TestSftpLocation\" $remotePath = "/Oresponces/" # Will continuously report progress of synchronization $session.add_FileTransferred( { FileTransferred($_) } ) # Connect $session.Open($sessionOptions) # Synchronize files $synchronizationResult = $session.SynchronizeDirectories( [WinSCP.SynchronizationMode]::Remote, $localPath, $remotePath, $False) # Throw on any error $synchronizationResult.Check() } finally { # Disconnect, clean up $session.Dispose() } exit 0 } catch [Exception] { Write-Host $_.Exception.Message exit 1 }