Upload file to webdav using Powershell

This is small powershell script to upload a file from your local disk to a webdav server. It is also proxy capable.

#Complete path of the file to be uploaded
$file = "d:\test_file.txt"
 
#URL without the last "/"
$url  = "https://YOUR-SERVER/webdav"
$proxy = "http=PROXY-SERVER:PORT"  
 
#User and Pwd for Webdav Access
$user = "USERNAME"
$pass = "PASSWORD"
 
$url += "/" + $file.split('\')[(($file.split("\")).count - 1)]

# Set binary file type
Set-Variable -name adFileTypeBinary -value 1 -option Constant 
 
$objADOStream = New-Object -ComObject ADODB.Stream
$objADOStream.Open()
$objADOStream.Type = $adFileTypeBinary
$objADOStream.LoadFromFile("$file")
$buffer = $objADOStream.Read()
$objXMLHTTP = New-Object -ComObject MSXML2.ServerXMLHTTP
$objXMLHTTP.setProxy(2, $proxy,"")
$objXMLHTTP.Open("PUT", $url, $False, $user, $pass)
$objXMLHTTP.send($buffer)

1 thought on “Upload file to webdav using Powershell

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.