June 4, 2009

FTP in Powershell

Here is an easy way to FTP in powershell:
$src = "ftp://ftp.microsoft.com/Services/Museum/pictures/windows1box.jpg"
$dest = "c:\temp\windows1box.jpg"
$Client = New-Object System.Net.WebClient
$Client.DownloadFile($src, $dest)
To upload a file, you need to change one line:
$Client.UploadFile($src, $dest)
Oh, Windows1 was a very long time ago!

8 comments:

  1. How do we give user name and password

    ReplyDelete
  2. Here is an example broken out to a nice function: http://powershell.com/cs/media/p/804.aspx

    ReplyDelete
  3. I need to post a file..How do I do that.I am new to PS.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. I was trying the above code, I do not quite understand what 3rd and 4th lines do?

    ReplyDelete
  6. Typo above. try this:

    $File = "D:\Dev\somefilename.zip"
    $ftp = "ftp://username:password@example.com/pub/incoming/somefilename.zip"

    $webclient = New-Object System.Net.WebClient
    $uri = New-Object System.Uri($ftp)

    $webclient.UploadFile($uri, $File)

    ReplyDelete
  7. If I have to do a SFTP then how do I give the port number?

    ReplyDelete