Skip to content

Instantly share code, notes, and snippets.

@marshyon
Last active January 30, 2022 19:50
Show Gist options
  • Save marshyon/cfb1c2da27f05f0865f34ade05f426c7 to your computer and use it in GitHub Desktop.
Save marshyon/cfb1c2da27f05f0865f34ade05f426c7 to your computer and use it in GitHub Desktop.
Powershell script to copy sharepoint files to a local directory using Powershell Core and PnP.PowerShell
# Input Parameters
# Install-Module -Name PnP.PowerShell -AllowClobber
$folder = "/Shared Documents"
# $backupDir = "C:/Users/USER/projects/powershell/backup"
function New-BackupDir {
$date = Get-Date
$dateString = $date.tostring("yyyy-MM-dd-HH")
$newBackupDir = $PSScriptRoot + "\backups-" + $dateString
$newBackupZip = $PSScriptRoot + "\archive-" + $dateString + ".zip"
if ( -Not ( Test-Path $newBackupDir ) ) {
Write-Host "creating a directory ... ["$PSScriptRoot "] "$newBackupDir " " $newBackupZip
$nDir = New-Item -ItemType Directory -Path $newBackupDir -Force
if ( $nDir.Exists ) {
Write-Host "created ok..."
}
else {
Write-Host "Error creating directory " $Error
}
# Compress-Archive -Path $PSScriptRoot -DestinationPath $newBackupZip
# encrypt / decrypt with
# https://4sysops.com/archives/encrypt-and-decrypt-files-with-powershell-and-pgp/
# Add-Encryption -FolderPath C:\Users\USER\projects\powershell\important\ -Password VERYSECRET
# Remove-Encryption -FolderPath C:\Users\USER\projects\powershell\important\ -Password VERYSECRET -Verbose
# create a windows link with
# powershell.exe -command "& 'C:\Users\USER\projects\powershell\make_dir_datestr.ps1' -MyArguments blah"
# script creation of the link with
# $WshShell = New-Object -comObject WScript.Shell
# $Shortcut = $WshShell.CreateShortcut("C:\Users\USER\projects\powershell\RunBackupScript.lnk")
# $Shortcut.TargetPath = "powershell.exe"
# $Shortcut.Arguments = "-command `"& 'C:\Users\USER\projects\powershell\make_dir_datestr.ps1' -MyArguments blah`""
# $Shortcut.Save()
}
return $newBackupDir
}
# Loop through to get all the folders and subfolders
Function GetFolders($folderUrl) {
$folderColl = Get-PnPFolderItem -FolderSiteRelativeUrl $folderUrl -ItemType Folder
# Loop through the folders
foreach ($folder in $folderColl) {
$newFolderURL = $folderUrl + "/" + $folder.Name
write-host "[" $newFolderURL "]"
$newBackupDir = $backupDir + $newFolderURL
$nDir = New-Item -ItemType Directory -Path $newBackupDir -Force
Write-Host "=> {" $newBackupDir "} " $nDir.CreationTime
$fNames = Get-PnPFolderItem -FolderSiteRelativeUrl $newFolderURL -ItemType File
foreach ($fName in $fNames) {
$fromFile = $newFolderURL + "/" + $fName.Name
$nfile = $newBackupDir + "/" + $fName.Name
Write-Host " < " $fromFile
Write-Host " > " $nfile
Get-PnPFile $fromFile -Path $newBackupDir -FileName $fName.Name -AsFile -Force
}
# Call the function to get the folders inside folder
GetFolders($newFolderURL)
}
}
# Connect to SharePoint Online site
Connect-PnPOnline -Url "https://SITENAME.sharepoint.com/sites/SITEURL" -Interactive -ForceAuthentication
# Call the functions
$backupDir = New-BackupDir
Write-Host "backup directory is now [$backupDir]"
GetFolders($folder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment