ArcherJayden

Subfolder

May 27th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. function FolderFromSharepoint(){
  2.  
  3. [CmdletBinding()]
  4. param(
  5. [Parameter(Position=0,mandatory=$true)]
  6. [string]$spVer,
  7. [string]$url,
  8. [string]$driveLtr,
  9. [string]$localPath)
  10.  
  11. BEGIN{
  12. cls
  13. $driveLtr = "Z"
  14. $localPath = "c:/temp/"
  15.  
  16. #Create PSDrive
  17. Connect-PnPOnline -Url https://contoso.sharepoint.com/sites/test -UseWebLogin -CreateDrive -DriveName $driveLtr
  18.  
  19. #Get All Items in a Folder
  20.  
  21. #$siteItems = (gci ($driveLtr + ':'))
  22. #Get All Items in a Folder
  23. $siteItems = (gci ($driveLtr + ':') -Recurse) | ?{$_.GetType().Name -eq 'Folder'} | Where {$_.Name -eq 'Shared Documents'}
  24.  
  25. #Separate Folders and Files
  26. #$folders = $siteItems | ?{$_.GetType().Name -eq 'SubFolder'} | Where {$_.Name -eq "General"}
  27. #$files = $siteItems | ?{$_.GetType().Name -eq 'File'}
  28.  
  29. }
  30. PROCESS{
  31. #Create Folder Start
  32. #$folders | ForEach-Object { New-Item -ItemType directory -path ($localPath + $_.ServerRelativeUrl) -force }
  33.  
  34.  
  35. #Copy Files Start
  36. #$files | ForEach-Object { Copy-Item ('Z:' + $_.ServerRelativeUrl) -Destination ($localPath + ($_.ServerRelativeUrl -replace $_.Name)) }
  37.  
  38.  
  39. #Create Root Directory
  40. $destFolder = New-Item -ItemType directory -path ($localPath + (Get-PSDrive $driveLtr).CurrentLocation) -force # | Out-Null
  41.  
  42. $siteItems | ? { $_.Name -eq 'General' } | % {
  43. $folder_item = $_
  44. #Get Full Path To Desired Folder Location
  45. write-host "Copying: $(($folder_item.PSParentPath -split '::')[1] + "\" + $folder_item.Name) to $(($localPath + (Get-PSDrive $driveLtr).CurrentLocation))"
  46. Copy-Item ((Get-PSDrive $driveLtr).Name + ":\" + $folder_item.ServerRelativeUrl ) -Destination ($localPath + (Get-PSDrive $driveLtr).CurrentLocation) -recurse -container
  47. }
  48.  
  49.  
  50. }
  51. END{
  52. #Remove PSDrive
  53. Get-PSDrive $driveLtr | Remove-PSDrive
  54. }
  55. }
  56.  
  57. FolderFromSharepoint -spVer 2016 -url 'https://sharepoint.contosos.com/{site}' -driveLtr $driveLtr -localPath $localPath
Add Comment
Please, Sign In to add comment