Geeks With Blogs

Lance's TextBox » About Me » My Resume »Twitter

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 : , , , , ,

Posted on Thursday, November 9, 2006 11:37 AM Software , PowerShell | Back to top


Comments on this post: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)
Requesting Gravatar...
I'm a complete PowerShell noob. How would I recursively upload a directory mutiple levels deep via send-ftp?
Left by ckj on Dec 05, 2006 6:03 PM

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)
Requesting Gravatar...
Left by Lance on Dec 07, 2006 12:03 PM

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)
Requesting 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.
Left by lb on Mar 29, 2007 8:33 PM

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)
Requesting 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.
Left by Lance on Mar 30, 2007 11:54 AM

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)
Requesting Gravatar...
How did you know the "-path" parameter exists? It's not documented anywhere and doesn't seem to like any value other than "/"
Left by Peter on Mar 10, 2008 8:05 PM

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)
Requesting Gravatar...
Hey Peter - actually, "Path" is an alias for the perhaps poorly named "List" parameter. It is a bit tricky and hard to find.
Left by Lance on Mar 11, 2008 9:12 AM

# re: ERROR Handling? : NetCmdlets Part 1: PowerShell cmdlet for FTP
Requesting 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!
Left by LenN on Oct 21, 2009 5:11 AM

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)
Requesting 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
Left by Lance on Oct 21, 2009 5:27 AM

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)
Requesting 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? :)
Left by Fernando Madruga on Apr 05, 2010 12:41 PM

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)
Requesting 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?
Left by sluice on Nov 29, 2010 6:07 AM

# re: NetCmdlets Part 1: PowerShell cmdlet for FTP (plus FTPS and SFTP)
Requesting 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
Left by mikew on Jun 17, 2011 8:36 PM

# Error handling on timeout
Requesting Gravatar...
I am getting timeout errors but the try-catch method isn't quite working out for me...

send-ftp -server $site -user $user -password $password -Force -SSL Explicit -LocalFile $filename -LogFile '\\xxx\Transmission.log' -Binary

These aren't stopping errors but just timeout and incompletes. Is there any handling for this?

Left by Ryan on Sep 08, 2014 12:52 PM

Your comment:
 (will show your gravatar)


Copyright © Lance Robinson | Powered by: GeeksWithBlogs.net