Last active
February 19, 2018 23:36
-
-
Save chrisbrownie/1ee513f4b0f9ebc76f0fe84d032c3cb9 to your computer and use it in GitHub Desktop.
Copies files from the local computer to an Office 365 Group
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
param( | |
[String] | |
$Path, #the path to the files to be uploaded | |
[String] | |
$siteUrl, #this is the URL of the Group in the format https://contoso.sharepoint.com/sites/yourGroupName | |
[String] | |
$listName = "Shared Documents" | |
#this is the path within the site to the place where you want to put the files | |
#the Groups 'files' list is 'Shared Documents' which is the default | |
) | |
Connect-PnPOnline -Url $siteUrl -UseWebLogin | |
$SourcePath = Get-Location | |
$AllFiles = Get-ChildItem $Path -Recurse -Force -ErrorAction SilentlyContinue | |
$filesCreated= @() | |
foreach ($file in $Allfiles) { | |
if ($file.GetType().Name -ne "DirectoryInfo") { | |
#$file.FullName | |
$filePath = [System.IO.Path]::GetDirectoryName($file.FullName)+"\" | |
$urlPath = $filepath.Replace($SourcePath,"").Replace("/","\") | |
$SpoPath = $listName+$urlPath | |
$FileUrl = $siteUrl + "/" + $listName + "/" + $file.FullName.Replace($pwd.ToString(),"").TrimStart("\").Replace("\","/") | |
try { | |
Get-PnPFile -Url $FileUrl -ErrorAction Stop | Out-Null | |
# If we got here, the file exists in the target location, do nothing | |
} catch { | |
# If we got here, the file doesn't exist in the target location, so let's create it | |
$filesCreated+=Add-PnPFile -Path $file.FullName -Folder $SpoPath -Values @{ | |
Modified = $file.LastWriteTime | |
Created = $file.CreationTime | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment