Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Upload files (recursive) via powershell
## Automate FTP uploads
## Go to destination
cd C:\Test
$location = Get-Location
"We are here: $location"
## Get files
$files = Get-ChildItem -recurse
## Get ftp object
$ftp_client = New-Object System.Net.WebClient
$ftp_address = "ftp://user:pass@adress:/home/chardcastle/test"
## Make uploads
foreach($file in $files)
{
$directory = "";
$source = $file.DirectoryName + "\" + $file;
if ($File.DirectoryName.Length -gt 0)
{
$directory = $file.DirectoryName.Replace($Location,"")
}
$Directory += "/";
$FtpCommand = $ftp_address + $directory + $file
$uri = New-Object System.Uri($ftp_command)
"Command is " + $uri + " file is $source"
$ftp_client.UploadFile($uri, $source)
}
@glukhovd

This comment has been minimized.

Copy link

@glukhovd glukhovd commented Apr 28, 2015

$FtpCommand and $ftp_command...

@fakir1hossain

This comment has been minimized.

Copy link

@fakir1hossain fakir1hossain commented Mar 13, 2017

This is a really great script. Helped me save a lot of time. Fakir Hossain

@johnsblac

This comment has been minimized.

Copy link

@johnsblac johnsblac commented May 13, 2017

Line 24, remove the underscore

@hostingrajamani

This comment has been minimized.

Copy link

@hostingrajamani hostingrajamani commented Aug 24, 2017

I get the following Error, can you please help ?

New-Object : A constructor was not found. Cannot find an appropriate
constructor for type System.Uri.
At C:\Script\ftp-upload.ps1:35 char:12

  • $uri = New-Object System.Uri($ftp_command)
    
  •        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentExce
      ption
    • FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.C
      ommands.NewObjectCommand
@hostingrajamani

This comment has been minimized.

Copy link

@hostingrajamani hostingrajamani commented Aug 24, 2017

This script is not working and wasted 1 hour, Use this script, https://www.kittell.net/code/powershell-ftp-upload-directory-sub-directories/ (it just works great) for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment