Created
March 31, 2023 20:29
-
-
Save RobBiddle/3fdecf8a66dae473be4e859b9b2678a1 to your computer and use it in GitHub Desktop.
Restore files deleted from a Microsoft Teams team
This file contains 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
<# | |
.SYNOPSIS | |
Restore files deleted in Microsoft Teams | |
.DESCRIPTION | |
Restore files deleted by a specific user on a specific date | |
.NOTES | |
PowerShell 7 only | |
Author: Robert D. Biddle | |
https://github.com/RobBiddle | |
#> | |
function Restore-TeamsFiles { | |
[CmdletBinding()] | |
param () | |
if (-NOT ($PSVersionTable).PSVersion.Major -GE 7) { | |
Throw "THIS MUST BE EXECUTED IN POWERSHELL 7" | |
} | |
if (-NOT (Get-Module -Name PnP.PowerShell -ListAvailable)) { | |
Install-Module PnP.PowerShell -Force | |
Import-Module -Name PnP.PowerShell -Force | |
} else { | |
Import-Module -Name PnP.PowerShell -Force | |
} | |
if (-NOT (Get-Module -Name MicrosoftTeams -ListAvailable)) { | |
Install-Module MicrosoftTeams -Force | |
Import-Module -Name MicrosoftTeams -Force | |
} else { | |
Import-Module -Name MicrosoftTeams -Force | |
} | |
if (-NOT (Get-Module -Name ExchangeOnlineManagement -ListAvailable)) { | |
Install-Module ExchangeOnlineManagement -Force | |
Import-Module -Name ExchangeOnlineManagement -Force | |
} else { | |
Import-Module -Name ExchangeOnlineManagement -Force | |
} | |
Connect-ExchangeOnline | |
Connect-MicrosoftTeams | |
While ($null -eq $Teams){$Teams = Get-Team} | |
$Team = $Teams | Out-GridView -Title "Select Team to Restore Files" -PassThru | |
$TeamUnifiedGroup = Get-Unifiedgroup -Filter {ResourceProvisioningOptions -eq "Team"} | Where-Object {$_.DisplayName -eq $Team.DisplayName -and $_.ExternalDirectoryObjectId -eq $Team.GroupId} | |
$user = Get-TeamUser -GroupId $Team.GroupId | Sort-Object Name | Out-GridView -Title "Select User whom Deleted Files" -PassThru | |
$TeamsSiteURL = $TeamUnifiedGroup.SharePointSiteUrl | |
$PnpConnection = Connect-PnPOnline -ReturnConnection -UseWebLogin -Url $TeamsSiteURL | |
$DeletedDate = Read-Host -Prompt "What Date were files deleted? yyyy-mm-dd" | |
$RestoreDate = Get-Date $DeletedDate | |
$RestoreUser = $user.User | |
$FilesToRestore = (Get-PnPRecycleBinItem -FirstStage -Connection $PnpConnection) | Where-Object { ($_.DeletedDate -gt $RestoreDate) -and ($_.DeletedDate -lt $RestoreDate.AddDays(1)) -and ($_.DeletedByEmail -eq $RestoreUser) } | |
$FilesToRestore += (Get-PnPRecycleBinItem -SecondStage -Connection $PnpConnection) | Where-Object { ($_.DeletedDate -gt $RestoreDate) -and ($_.DeletedDate -lt $RestoreDate.AddDays(1)) -and ($_.DeletedByEmail -eq $RestoreUser) } | |
$FilesToRestore | ForEach-Object {$_ | Restore-PnpRecycleBinItem -Force -Connection $PnpConnection -ErrorAction Continue} | |
} | |
Restore-TeamsFiles |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment