question

ChrisManceyGroup-6193 avatar image
0 Votes"
ChrisManceyGroup-6193 asked MichaelHan-MSFT edited

Delete a specific sharepoint external share using powershell

I have a SharePoint library in an O365 tenant with a large number of files/folders. The files have been shared externally with customers over the years and now we have reached to threshold limit for sharing.

We need to remove any shares for external guests that are older than 1 month

Using PowerShell, here's what I have so far

  ###### Declare and Initialize Variables ######  
  $url="<siteURL>"  
  $listName="Sharing Links"  
  $currentTime= $(get-date).ToString("yyyyMMddHHmmss")  
  $logFilePath=".\logs"+$currentTime+".txt"  
  # Fields that has to be retrieved  
  $Global:selectProperties=@("SharingDocId", "AvailableLinks","ShareId","AuthKey","RoleDefinitionId","Invitees","Created");  
  $DaysCutOff = 31
      
  ## Start the Transcript  
  Start-Transcript -Path $logFilePath
      
  ## Export List to CSV ##  
  function ExportList  
  {  
      try  
      {  
          # Get all list items using PnP cmdlet  
          $listItems=(Get-PnPListItem -List $listName -Fields $Global:selectProperties).FieldValues
          $outputFilePath="C:\scripts\results-"+$currentTime+".csv"  
       
          $hashTable=@()  
      
          # Loop through the list items  
          foreach($listItem in $listItems)  
          {   
              $obj=New-Object PSObject              
              $listItem.GetEnumerator() | Where-Object { $_.Key -in $Global:selectProperties }| ForEach-Object{ $obj | Add-Member Noteproperty $_.Key $_.Value}  
              $hashTable+=$obj;  
              $obj=$null;  
              if ($listItem.Created -lt (Get-Date).AddDays(-$DaysCutOff) ){
                  write-host "Share Older that $DaysCutOff days"
     
                  #Delete the share <-- not sure what goes here to delete the share
              }
          }  
       
          $hashtable | export-csv $outputFilePath -NoTypeInformation  
       }  
       catch [Exception]  
       {  
          $ErrorMessage = $_.Exception.Message         
          Write-Host "Error: $ErrorMessage" -ForegroundColor Red          
       }  
  }  
      
  ## Connect to SharePoint Online site  
  Connect-PnPOnline -Url $url -UseWebLogin  
     
      
  ## Call the Function  
  ExportList  
      
  ## Disconnect the context  
  Disconnect-PnPOnline  
      
  ## Stop Transcript  
  Stop-Transcript  

Not all global variable get values but it works so far i just don't know how to delete the share


If it look at the variable for $listitem I can see the values

134599-image.png

Is it possible to delete base on the ShareingDocID or UniqueID fields?

Any help would be appreciated.


office-sharepoint-online
image.png (31.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MichaelHan-MSFT avatar image
0 Votes"
MichaelHan-MSFT answered MichaelHan-MSFT edited

Hi @ChrisManceyGroup-6193,

You could delete base on the ID filed. Use Remove-PnPListItem cmdlet by passing item id to delete it:

 Remove-PnPListItem -List $listName -Identity "<ID>"


If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.