The loop (or even better a recursion) is the only way to do this natively in PowerShell (or .NET in general).
$source = "c:source"
$destination = "ftp://username:password@example.com/destination"
$webclient = New-Object -TypeName System.Net.WebClient
$files = Get-ChildItem $source
foreach ($file in $files)
{
Write-Host "Uploading $file"
$webclient.UploadFile("$destination/$file", $file.FullName)
}
$webclient.Dispose()
Note that the above code does not recurse into subdirectories.
If you need a simpler solution, you have to use a 3rd party library.
For example with WinSCP .NET assembly:
Add-Type -Path "WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.ParseUrl("ftp://username:password@example.com/")
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
$session.PutFiles("c:source*", "/destination/").Check()
$session.Dispose()
The above code does recurse.
See https://winscp.net/eng/docs/library_session_putfiles
(I'm the author of WinSCP)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…