Basic Sharepoint Online Upload and Download File
A quick beginner's guide to using SharePoint Online to upload and download a file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Comments
Post a Comment