Last active
March 28, 2025 12:10
-
-
Save OscarValerock/375531e488008612d2938ec8765cdce0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| This Gist contains scripts to learn SharePoing PnP Power Shell |
This file contains hidden or 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 PS Module --- *-*-*-*-*-*# | |
| #For using all this shells you require to install the PnP PowerShell module. | |
| Install-Module -Name PnP.PowerShell |
This file contains hidden or 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
| #Define variables | |
| $siteUrl = "https://<SharePoint site>/" | |
| $listName = "List Name" |
This file contains hidden or 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
| #*-*-*-*-*-*--- Connect to the target SharePoint --- *-*-*-*-*-*# | |
| #You always require to connect to the targe SharePoint, this can be done in a couple of ways: | |
| #Also, let's start using variables; in this case the first varible will hold the SharePoint site URL | |
| $siteUrl = "https://<SharePoint site>" | |
| Connect-PnPOnline -Url $siteUrl -Interactive | |
| Connect-PnPOnline -Url $siteUrl -UseWebLogin |
This file contains hidden or 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
| #Define variables and connect to SharePoint | |
| $siteUrl = "https://<SharePoint site>/" | |
| $listName = "List Name" | |
| Connect-PnPOnline -Url $siteUrl -UseWebLogin |
This file contains hidden or 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
| #Create a new list | |
| #https://www.sharepointdiary.com/2014/12/sharepoint-online-powershell-to-create-list.html | |
| New-PNPList -Title $listName -Template “genericlist” -OnQuickLaunch |
This file contains hidden or 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
| #Add fields to the new list | |
| Add-PNPField -List $listName -type Text -DisplayName "Color" -InternalName "Color" -AddToDefaultView | |
| Add-PNPField -List $listName -type Text -DisplayName "Animal" -InternalName "Animal" -AddToDefaultView | |
| Add-PNPField -List $listName -type User -DisplayName "Owner" -InternalName "Owner" -AddToDefaultView |
This file contains hidden or 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
| #Understand the "For" loop | |
| #https://www.oreilly.com/library/view/mastering-windows-powershell/9781787126305/f02af2bc-3ea5-45f9-a7ac-31461f1753f4.xhtml | |
| for ($i = 0; $i -le 1000; $i++) { | |
| Write-Host $i } |
This file contains hidden or 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
| #Fill in the list with ten random elements v1.0 | |
| $total = 10 | |
| $progressStep = 100/$total | |
| for ($i=1; $i -le $total; $i++) | |
| { | |
| Add-PNPListItem -List $listName -Values @{"Title" = "Item Title " + $i; "Color" = "Color-$i"; "Animal" = "Animal-$i"} -ContentType "Item" | |
| $currentProgress = ($i)*$progressStep | |
| Write-Progress -Id 1 -Activity "Creating" -Status "$i/$total Complete:" -PercentComplete $currentProgress | |
| } |
This file contains hidden or 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
| #Fill in the list with ten random elements v2.0 | |
| $total = 10 | |
| $progressStep = 100/$total | |
| for ($i=1; $i -le $total; $i++) | |
| { | |
| $randomColor = "Blue","Red","Green" | Get-Random | |
| $randomAnimal = "Cat","Lion","Bird" | Get-Random | |
| Add-PNPListItem -List $listName -Values @{"Title" = "Item Title " + $i; "Color" = $randomColor; "Animal" = $randomAnimal; "Owner" = "contact@bibb.pro"} -ContentType "Item" | |
| $currentProgress = ($i)*$progressStep | |
| Write-Progress -Id 1 -Activity "Creating" -Status "$i/$total Complete:" -PercentComplete $currentProgress | |
| } |
This file contains hidden or 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
| #Delete all elements from the list | |
| #https://www.spguides.com/remove-all-items-from-a-sharepoint-online-list-using-pnp-powershell/ | |
| #https://communary.net/2015/01/19/how-to-add-a-progress-bar-to-your-powershell-script/ | |
| #https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-progress?view=powershell-7.2 | |
| $items =Get-PnPListItem -List $listName -PageSize 500 | Where-Object {$_["Color"] -ne $null} | |
| $totalItems = $items.Count | |
| $progressStep = 100/$totalItems | |
| $counter = 0 | |
| foreach ($item in $items) | |
| { | |
| try | |
| { | |
| $counter++ | |
| $currentProgress = ($counter)*$progressStep | |
| Remove-PnPListItem -List "$listName" -Identity $item.Id -Force | |
| Write-Progress -Id 1 -Activity "Deleting" -Status "$counter/$totalItems Complete:" -PercentComplete $currentProgress | |
| } | |
| catch | |
| { | |
| Write-Host "Error Occurred While Deleting the Item from the SharePoint Online List" | |
| } | |
| } |
This file contains hidden or 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
| #Delete the list | |
| #https://www.sharepointdiary.com/2016/10/sharepoint-online-delete-list-using-powershell.html | |
| #Check if List exists | |
| If(Get-PnPList -Identity $listName ) | |
| { | |
| #sharepoint online powershell remove list | |
| Remove-PnPList -Identity $listName -Force #Use the “-Recycle” switch to send the list or library to recycle bin! | |
| Write-host -f Green "List $listName' Deleted Successfully!" | |
| } | |
| Else | |
| { | |
| Write-host -f Yellow "Could not find List '$listName'" | |
| } | |
This file contains hidden or 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
| #*-*-*-*-*-*--- Create a new list and fill it with ten random elements, count elements and delete it --- *-*-*-*-*-*# | |
| #We will create a SharePoint list using PowerShell and add some new fields into the PowerShell. | |
| #Connect to SharePoint | |
| $siteUrl = "https://<SharePoint site>/" | |
| $listName = "List Name" | |
| Connect-PnPOnline -Url $siteUrl -UseWebLogin | |
| #Create a new list | |
| #https://www.sharepointdiary.com/2014/12/sharepoint-online-powershell-to-create-list.html | |
| New-PNPList -Title $listName -Template “genericlist” -OnQuickLaunch | |
| #Add fields to the new list | |
| Add-PNPField -List $listName -type Text -DisplayName "Color" -InternalName "Color" -AddToDefaultView | |
| Add-PNPField -List $listName -type Text -DisplayName "Animal" -InternalName "Animal" -AddToDefaultView | |
| Add-PNPField -List $listName -type User -DisplayName "Owner" -InternalName "Owner" -AddToDefaultView | |
| #Understand the "For" loop | |
| #https://www.oreilly.com/library/view/mastering-windows-powershell/9781787126305/f02af2bc-3ea5-45f9-a7ac-31461f1753f4.xhtml | |
| for ($i = 0; $i -le 1000; $i++) { | |
| Write-Host $i } | |
| #Fill in the list with ten random elements v2.0 | |
| $total = 10 | |
| $progressStep = 100/$total | |
| for ($i=1; $i -le $total; $i++) | |
| { | |
| $randomColor = "Blue","Red","Green" | Get-Random | |
| $randomAnimal = "Cat","Lion","Bird" | Get-Random | |
| Add-PNPListItem -List $listName -Values @{"Title" = "Item Title " + $i; "Color" = $randomColor; "Animal" = $randomAnimal; "Owner" = "contact@bibb.pro"} -ContentType "Item" | |
| $currentProgress = ($i)*$progressStep | |
| Write-Progress -Id 1 -Activity "Creating" -Status "$i/$total Complete:" -PercentComplete $currentProgress | |
| } | |
| #Delete all elements from the list | |
| #https://www.spguides.com/remove-all-items-from-a-sharepoint-online-list-using-pnp-powershell/ | |
| #https://communary.net/2015/01/19/how-to-add-a-progress-bar-to-your-powershell-script/ | |
| #https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-progress?view=powershell-7.2 | |
| $items =Get-PnPListItem -List $listName -PageSize 500 | Where-Object {$_["Color"] -ne $null} | |
| $totalItems = $items.Count | |
| $progressStep = 100/$totalItems | |
| $counter = 0 | |
| foreach ($item in $items) | |
| { | |
| try | |
| { | |
| $counter++ | |
| $currentProgress = ($counter)*$progressStep | |
| Remove-PnPListItem -List "$listName" -Identity $item.Id -Force | |
| Write-Progress -Id 1 -Activity "Deleting" -Status "$counter/$totalItems Complete:" -PercentComplete $currentProgress | |
| } | |
| catch | |
| { | |
| Write-Host "Error Occurred While Deleting the Item from the SharePoint Online List" | |
| } | |
| } | |
| #Delete the list | |
| #https://www.sharepointdiary.com/2016/10/sharepoint-online-delete-list-using-powershell.html | |
| #Check if List exists | |
| If(Get-PnPList -Identity $listName ) | |
| { | |
| #sharepoint online powershell remove list | |
| Remove-PnPList -Identity $listName -Force #Use the “-Recycle” switch to send the list or library to recycle bin! | |
| Write-host -f Green "List $listName' Deleted Successfully!" | |
| } | |
| Else | |
| { | |
| Write-host -f Yellow "Could not find List '$listName'" | |
| } | |
This file contains hidden or 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
| $siteUrl = "https://<SharePoint site>/" | |
| Connect-PnPOnline -Url $siteUrl -UseWebLogin | |
| # Define the library name | |
| $LibraryName = "UserProfilesTestDelete3" | |
| # Create the Document Library | |
| New-PnPList -Title $LibraryName -Template DocumentLibrary | |
| # Define fields to add | |
| $fields = @( | |
| @{ DisplayName = "UserName"; InternalName = "UserName"; Type = "Text" }, | |
| @{ DisplayName = "UserJobTitle"; InternalName = "UserJobTitle"; Type = "Text" }, | |
| @{ DisplayName = "UserEmail"; InternalName = "UserEmail"; Type = "Text" }, | |
| @{ DisplayName = "UserPhone"; InternalName = "UserPhone"; Type = "Text" }, | |
| @{ DisplayName = "UserBio"; InternalName = "UserBio"; Type = "Note" }, | |
| @{ DisplayName = "YearsExperience"; InternalName = "YearsExperience"; Type = "Text" }, | |
| @{ DisplayName = "Expertise"; InternalName = "Expertise"; Type = "Text" }, | |
| @{ DisplayName = "Quote"; InternalName = "Quote"; Type = "text" }, | |
| @{ DisplayName = "Page"; InternalName = "Page"; Type = "Number" }, | |
| @{ DisplayName = "Order0"; InternalName = "Order0"; Type = "Number" }, | |
| @{ DisplayName = "UserUrl"; InternalName = "UserUrl"; Type = "Text" } | |
| ) | |
| # Add fields using a loop with default view update | |
| foreach ($field in $fields) { | |
| Add-PnPField -List $LibraryName ` | |
| -DisplayName $field.DisplayName ` | |
| -InternalName $field.InternalName ` | |
| -Type $field.Type ` | |
| -AddToDefaultView | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment