NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)

Some examples of using NetCmdlets in PowerShell. The FTP NetCmdlet supports plain and secure (both SSL and SSH) FTP connections right from the PowerShell console.

List files on a remote server:

PS C:\> get-ftp -server myserver -user lancer -password mypass

The same, but using SSL (auth-tls):

PS C:\> get-ftp -server myserver -user lancer -password mypass -ssl explicit

And again, but using SSH 2.0:

PS C:\> get-ftp -server myserver -user lancer -password mypass -ssh

Find the files on a remote server that are larger than 10 Megs:

PS C:\> get-ftp -server myserver -user lancer -password mypass| where { $_.FileSize -gt 10MB }

Delete a file on a remote FTP server:

send-ftp -server myserver -user lancer -password mypass -delete filetodelete.txt

Delete all files on a remote FTP server that are greater than 10 Megs:
PS C:\>get-ftp -server myserver -user lancer -password mypass | where { $_.FileSize -gt 10MB } | foreach-object -process {send-ftp -server myserver -user lancer -password mypass -delete $_.FileName}

Calculate the number of bytes in the files of a remote server directory:

PS C:\> get-ftp -server myserver -user lancer -password mypass -path / | measure-object -property FileSize -sum

Find all the local .txt files and upload them to a remote FTP server:

PS C:\> get-childitem *.txt | select Name | set-ftp -server myserver -user lancer -password mypassword -localfile $_ -remotefile $_

Technorati : , , , , ,

Print | posted on Thursday, November 09, 2006 11:37 AM

Feedback

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)

Left by ckj at 12/5/2006 6:03 PM
Gravatar I'm a complete PowerShell noob. How would I recursively upload a directory mutiple levels deep via send-ftp?

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)

Left by Lance at 12/7/2006 12:03 PM

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)

Left by lb at 3/29/2007 8:33 PM
Gravatar love powershell, love netcmdLets and love the info you've provided here lance.

but i can't workout if netCmdLets is a trial, or freeware, or what? The EULA seemed to cover all bases.

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)

Left by Lance at 3/30/2007 11:54 AM
Gravatar NetCmdlets is currently in a free beta state. Final licensing details aren't yet finalized by teh company, but I believe eventually some functionality will remain free and some will require some sort of licensing.

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)

Left by Peter at 3/10/2008 8:05 PM
Gravatar How did you know the "-path" parameter exists? It's not documented anywhere and doesn't seem to like any value other than "/"

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)

Left by Lance at 3/11/2008 9:12 AM
Gravatar Hey Peter - actually, "Path" is an alias for the perhaps poorly named "List" parameter. It is a bit tricky and hard to find.

# re: ERROR Handling? : NetCmdlets Part 1: PowerShell cmdlet for FTP

Left by LenN at 10/21/2009 5:11 AM
Gravatar One of the issues with FTP, is its failure to report %errorlevel%. While I've gotten around it clumsily by parsing the output for errors, I'm wondering whether this DLL will return a valid return code, if so what are they; or whether powershell will detect failures.

Additionally, can you pass instream FTP commands or from a script, rather than connect-per-command-disconnect?

Great work, btw!

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)

Left by Lance at 10/21/2009 5:27 AM
Gravatar Lenn,

You might want to email me directly so that we can get into the details, but everything you're asking for should already be there:

1. if a fatal error happens with get-ftp or send-ftp, the error will be passed on, so you can use regular powershell error handling techniques to get them (trap, finally, etc).

2. Yes, you can send multiple commands over the same connection by using the connect-ftp and disconnect-ftp cmdlets. Like this:

$ftp = connect-ftp -server myserver -user user1 -password mypassword
get-ftp -connection $ftp
...
disconnect-ftp -connection $ftp

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)

Left by Fernando Madruga at 4/5/2010 12:41 PM
Gravatar Delete all files on a remote FTP server that are greater than 10 Megs:
PS C:\>get-ftp -server myserver -user lancer -password mypass| foreach-object -process {send-ftp -server myserver -user lancer -password mypass -delete $_.FileName}

Uh? Is it me or will this instead delete ALL files regardless of size? :)

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)

Left by sluice at 11/29/2010 6:07 AM
Gravatar The way that most ftp ptograms do it is to create a database file of the local files and another of the remote. Compare the two, remove common files from the join and download the newer files in the database. Personally, I would use WinSCP.com command line; it's free and why reinvent the wheel?

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)

Left by mikew at 6/17/2011 8:36 PM
Gravatar Hi Lance, wondering if you found any issues with ftp server response (confirm you wish to etc...Press{Y},{N})
With ftp.exe I dont experience this, but send-ftp I can see passive mode connection details in filezilla - is it possible to side-step this challenge response?
Cheers

Your comment:





 
 

Copyright © Lance Robinson

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski