Feedback by UserVoice

Chris H

My feedback

  1. 465 votes
    Vote
    Sign in
    (thinking…)
    Sign in with: Facebook Google
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    16 comments  ·  Office 365 Admin » SharePoint Admin  ·  Flag idea as inappropriate…  ·  Admin →
    An error occurred while saving the comment
    Chris H commented  · 

    After searching high and low for a quick way to clean this all up, I wasn't able to find one, so I ended up coming up with a PowerShell script to remove all the files in the Preservation Hold Library.

    PowerShell modules used:
    AzureADPreview – Version 2.0.2.89
    Microsoft.Online.SharePoint.PowerShell – Version 16.0.19927
    SharePointPnPPowerShellOnline – Version 3.20.2004
    Commands were run under PowerShell 5.1.

    ####
    # Import the modules:
    Import-Module -Name AzureADPreview
    Import-Module -Name Microsoft.Online.SharePoint.PowerShell
    Import-Module -Name SharePointPnPPowerShellOnline

    # Enter global admin credentials
    $Credentials = Get-Credentials

    # Enter affected users upn
    $UserUPN = Read-Host " Enter affected users upn "

    # Connect to AzureAD
    Connect-AzureAD -Credential $Credentials

    # Obtain tenant name
    $InitialDomain = Get-AzureADDomain | Where-Object {$_.IsInitial -eq $true}
    $TenantName = $InitialDomain.Name.Substring(0, $InitialDomain.Name.IndexOf('.'))

    # Connect to SharePoint Online Admin
    $SharePointAdminURL = "https://$TenantName-admin.sharepoint.com"
    Connect-SPOService -Url $SharePointAdminURL -Credential $Credentials

    # Set path to user's OneDrive
    $UserUnderscore = $UserUPN -replace "[^a-zA-Z]", "_"
    $userOneDriveSite = "https://$TenantName-my.sharepoint.com/personal/$UserUnderscore"

    # Set current admin as a Site Collection Admin on user's OneDrive Site Collection
    Set-SPOUser -Site $userOneDriveSite -LoginName $Credentials.UserName -IsSiteCollectionAdmin $true

    # Connect to user's OneDrive via SharePoint Online PNP module
    Connect-PnPOnline -Url $userOneDriveSite -Credentials $Credentials

    # Return all items in the Preservation Hold Library and remove them
    Get-PnPListItem -List 'Preservation Hold Library' -PageSize 1 | ForEach-Object {
    $Filename = $_.FieldValues.FileLeafRef
    Write-Host "Removing $Filename" -ForegroundColor Cyan
    Remove-PnPListItem -List 'Preservation Hold Library' -Identity $_ -Force
    }
    # Remove Global Admin from Site Collection Admin role for both users
    Set-SPOUser -Site $userOneDriveSite -LoginName $Credentials.UserName -IsSiteCollectionAdmin $false

Feedback and Knowledge Base