SFTP for PowerShell tutorial

Setup

The setup of SFTP commands for PowerShell is done with a Setup Wizard which copies documentation files in %ProgramFiles%\NCiphers SFTP commands for PowerShell and the compiled cmdlets inside %ProgramFiles%\WindowsPowerShell\Modules\NCiphers.SFTP.PowerShell.

Connecting

The first two parameters of each cmdlet provided by SFTP commands for PowerShell are used for establishing the SFTP connection.

Get-SftpChildItem -Host mysftphost.com -Port 22 ...

The -Host parameter expects either IP address (e.g. 192.168.0.1) or a domain name. The -Port parameter (optional, default is 22) is the SFTP server port number.

The next parameter is the -Username (-User) of the remote SFTP account:

Get-SftpChildItem -Host mysftphost.com -Port 22 -Username myuser ...

And then follows either password for the user account or SSH key and optional password that unlocks the key

Connecting with password

The password is provided with the -Password (-Pass) parameter

Get-SftpChildItem -Host mysftphost.com -Port 22 -Username myuser -Password mypass ...

Connecting with SSH key

OpenSSH, PuTTY and ssh.com type of keys are supported through the -KeyFile and -KeyPassword (or -KeyPass) parameters

Get-SftpChildItem -Host mysftphost.com -Port 22 -Username myuser -KeyFile c:\my.key -KeyPass keypassword

Common parameters

In addition to the connection parameters mentioned above, SFTP commands for PowerShell uses these two parameters for identifying remote object on the SFTP side and local resources:

-Path (or -LocalPath) – local file or folder

-RemoteLocation (or -Remote) – remote file or folder path

Depending on the SFTP command the -Path parameter may not be supported if not applicable.

Working with remote folders

Listing the contents of a remote folder

The Get-SftpChildItem cmdlet is used for obtaining the contents of a remote folder.

$files =  Get-SftpChildItem -Host mysftphost.com -User myuser -KeyFile c:\my.key -KeyPass keypassword -RemoteLocation "\TestFolder"
ForEach ($f in $files)
{
 Write-Host $f
}

Creating folder

The create command is useful for creating an empty remote folder. After that we can upload files inside it.

New-SftpDirectory -Host mysftphost.com -Port 22 -Username myuser -KeyFile c:\my.key -KeyPassword keypass -Path "c:\Test\inc\" -RemoteLocation TestFolder

Uploading folder

The first upload of local folder to a remote SFTP site will create the folder and upload the local files (and sub folders). Subsequent calls when the remote folder already exist, will synchronize the remote files with the local ones.

Send-SftpDirectory -Host mysftphost.com -Port 22 -User myuser -KeyFile c:\my.key -KeyPass keypass -Path "c:\Test\inc\" -RemoteLocation TestFolder

Downloading SFTP folder

Downloading remote folder will create will create the folder and upload the local files (and sub folders). Subsequent calls when the remote folder already exist, will synchronize the local files with the remote ones.

Receive-SftpDirectory -Host mysftphost.com -Port 22 -Username myuser -KeyFile c:\my.key -KeyPassword keypass -Path "c:\Test\inc\" -RemoteLocation TestFolder

Delete SFTP folder

Deleting remote SFTP folder deletes the remote folder with all its contents (files and sub folders)

Remove-SftpItem -Host mysftphost.com -Port 22 -Username myuser -KeyFile c:\my.key -KeyPassword keypass -RemoteLocation "\TestFolder"

Working with remote files

Uploading file

When uploading if the -RemoteLocation parameter is omitted the file will be uploaded to the SFTP account root folder:

Send-SftpFile -Host mysftphost.com -Port 22 -User myuser -KeyFile c:\my.key -KeyPass keypass -Path "c:\install.ini" -Remote "TestFolder\install.ini"

Downloading file

If the -Path parameter (it is optional) is skipped, the specified remote file will be downloaded to the current working directory.

Receive-SftpFile -Host mysftphost.com -Port 22 -User myuser -KeyFile c:\my.key -KeyPass keypass -Path "c:\TMP\install.ini" -Remote "TestFolder\install.ini"

Deleting file

The same command is used for deleting SFTP files and folders Remove-SftpItem

Remove-SftpItem -Host mysftphost.com -Port 22 -User myuser -KeyFile c:\my.key -KeyPass keypass -Remote "TestFolder\install.ini"

Inspecting remote files and folders

Is this a file or folder

$isFile = Get-SftpIsFile -Host mysftphost.com -Port 22 -Username myuser -KeyFile c:\my.key -KeyPassword keypass -RemoteLocation "install.ini"
$isFolder = Get-SftpIsDirectory -Host mysftphost.com -Port 22 -Username myuser -KeyFile c:\my.key -KeyPassword keypass -RemoteLocation "install.ini"
if ($isFile -eq $True)
{
Write-Host "install.ini is file"
}
elseif ($isFolder -eq $True)
{
Write-Host "install.ini is folder"
}

Exists

Get-SftpExists checks if a remote item with a specified name exists on the SFTP host

$existsTestFolder = Get-SftpExists -Host mysftphost.com -Port 22 -User myuser -KeyFile c:\my.key -KeyPass keypass -RemoteLocation "TestFolder"
if ($existsTestFolder -eq $True)
{
  Write-Host "TestFolder exists"
}
elseif ($existsTestFolder -eq $True)
{
  Write-Host "TestFolder does not exists"
}

Last access time

The last access time is when a file or folder were last accessed

$time = Get-SftpLastAccessTime -Host mysftphost.com -Port 22 -User myuser -KeyFile c:\my.key -KeyPass keypass -RemoteLocation "install.ini"
Write-Host "Last access time is $time"

Last write time

The last write time is when a file or folder were last written to

$time = Get-SftpLastWriteTime -Host mysftphost.com -Port 22 -User myuser -KeyFile c:\my.key -KeyPass keypass -RemoteLocation "install.ini"
Write-Host "Last write time is $time"

Help

Help for the syntax of SFTP commands for PowerShell can be obtained by invoking Get-Help like

Get-Help Get-SftpLastWriteTime

or in the CHM Help file that ships with the product.

Uninstall

In order to remove SFTP commands for PowerShell right click over SFTP commands for PowerShell in Windows Control Panel/Programs and Features and select Uninstall.

Summary

This online tutorial illustrates the usage of NCiphers SFTP commands for PowerShell. Connecting to remote SFTP servers is described along with common SFTP operations like working with remote files and folders, and sending data in both directions.

List of commands

  • Get-SftpChildItem – lists contents of SFTP directory
  • Get-SftpExists – checks if SFTP file or directory exist
  • Get-SftpFileSize – SFTP file size
  • Get-SftpIsFile – is SFTP file
  • Get-SftpIsDirectory – is SFTP directory
  • Get-SftpLastWriteTime – SFTP file last write time
  • Get-SftpLastAccessTime – SFTP file last access time
  • Send-SftpFile – upload SFTP file
  • Receive-SftpFile – download SFTP file
  • New-SftpDirectory – create SFTP directory
  • Send-SftpDirectory – upload SFTP directory
  • Receive-SftpDirectory – download SFTP directory
  • Remove-SftpItem – remove SFTP file or directory