Basic Sharepoint Online Upload and Download File

A quick beginner's guide to using SharePoint Online to upload and download a file.

# Install pre-requisite module
# ------------------------------------------
Install-Module -Name SharePointPnPPowerShellOnline
# Alternatively manually importing module
# instructions are at https://github.com/pnp/PnP-PowerShell/releases
# ------------------------------------------
Save-Module -Name SharePointPnPPowerShellOnline -Path C:\TEMP
Import-Module C:\TEMP\SharePointPnPPowerShellOnline\3.26.2010.0\SharePointPnPPowerShellOnline.psd1 -DisableNameChecking
# Connect to SP Online
# ------------------------------------------
$O365ServiceAccount = "<useraccount>"
$O365ServiceAccountPwd = "<password>"
$SharePointSiteURL = "https://<site>.sharepoint.com/sites/"
[SecureString]$SecurePass = ConvertTo-SecureString $O365ServiceAccountPwd -AsPlainText -Force
[System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass)
# Connecting to SharePoint Online site
Connect-PnPOnline -Url $SharePointSiteURL -Credentials $PSCredentials
# Download file
# ------------------------------------------
$sharedDriveFolderPath = "Shared Documents\TestFolderForTraining"
$files = Get-PnPFolderItem -FolderSiteRelativeUrl "$sharedDriveFolderPath" -ItemType File
ForEach ($file in $files) {
If ($($file.Name) -eq "this is a test.docx") {
Get-PnPFile -Url $file.ServerRelativeUrl -Path "$sharedDriveFolderPath" -FileName "c:\temp\$($File.Name)" -AsFile
}
}
# Upload file
# ------------------------------------------
Add-PnPFile -Path "c:\temp\newuseroutput.txt" -Folder $sharedDriveFolderPath
view raw gistfile1.txt hosted with ❤ by GitHub

Comments

Popular posts from this blog

Powershell Install-Module Error - Unable to Download from URI ... 0x409

Configuring App-only Authentication for Exchange Online for Powershell Scripts