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.Type = $adFileTypeBinary
$buffer = $objADOStream.Read()
$objXMLHTTP = New-Object -ComObject MSXML2.ServerXMLHTTP
$objXMLHTTP.setProxy(2, $proxy,"")
$objXMLHTTP.Open("PUT", $url, $False, $user, $pass)

4 thoughts on “Upload file to webdav using Powershell

  1. Is there a new way to use line 25?
    $objXMLHTTP.Open(“PUT”, $url, $False, $user, $pass) isn’t working:

    + $objXMLHTTP.Open(“POST”, $url, $False, $user, $pass)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

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.