Delete List SharePoint Online via Power-shell and Enabled Execution-Policy

Tools: SharePoint Online Management Shell Case: Want to delete a List or Library with Power-shell in SharePoint Online, and execute ...

logo - afahru.com


Tools:
SharePoint Online Management Shell

Case:
Want to delete a List or Library with Power-shell in SharePoint Online, and execute script on my environment. But, I got some little trouble. see below image for details:
SPO - afahru.com

Uppss, what is that?  Messages error: “Running scripts is disabled on this system”
Ok, let’s fix this issue. See below solutions.

Solutions:
Copy script below and save as with executable (dot)ps1, and open your SharePoint Online Management Shell with privileges as Administrator. Copy this below script:
Set-ExecutionPolicy RemoteSigned
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Set Variables for Site URL and List Name
$SiteURL= "https://youcompany.sharepoint.com/"
$ListName="Your Library Name"
 
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
 
    #Get the List
    $List=$Ctx.Web.Lists.GetByTitle($ListName)
 
    #Delete the list
    $List.DeleteObject()
    $Ctx.ExecuteQuery()
         
    Write-host -f Green "List Deleted Successfully!"
}
Catch {
    write-host -f Red "Error deleting list!" $_.Exception.Message
}
Locate to your file, and type this first “Set-ExecutionPolicy RemoteSigned” like below image:
policy - afahru.com

Then, execute your file [.\DeleteList.ps1] like below:
success - afahru.com
Done, hope this helpful 😊

You Might Also Like

0 comments