Nextcloud Logo

How to upload files to Nextcloud with PowerShell

After a few hours of Google search, I could find only one article (here) about this topic and that one was not working anymore. I grabbed that script and I modified it, fixed it and made it better.
First, you have to create/share the folder in the Nextcloud. Share it with a link.
Once you shared it, copy the link and paste it in a notepad. Grab the token number that is at the end of the link.
e.g https://cloud.contorso.com/s/6o4kp4n9fS6QAN8
That is the token you will be using in the script below.
Save the below script with .ps1 extension and enjoy.
# Enable TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# Define your Nextcloud address
$NextcloudUrl = "https://cloud.contorso.com/"

# Define the shared token got from the shared link
$sharetoken = "ChangeMe"

# Define folder path where are the files you would like to upload. It can upload only files not folders.
# It doens't know about override, will create a duplicate in case you run it twice and the same file name is there.
$filepath = "C:\Users\Username\Desktop\Folder"

# Getting all the files in the specified folder
$Item = Get-ChildItem -Recurse $filepath | Sort-Object fullname | Select FullName

# Will process each file individually and upload them to the cloud.
$Item | ForEach-Object {

    $file = $_.FullName

$Item = Get-Item $file

$Headers = @{
    "Authorization"=$("Basic $([System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($("$($sharetoken):"))))");
$webdav = "$($NextcloudUrl)/public.php/webdav/$($Item.Name)"
Invoke-RestMethod -Uri $webdav -InFile $Item.Fullname -Headers $Headers -Method Put
Picture of Adrian


Senior Engineer at Computer Assistance Oxford

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 3

No votes so far! Be the first to rate this post.

We are sorry that this post was not very useful for you!

Let us improve this post!

Tell us how we can improve this post?

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.