Last active
October 14, 2019 14:01
-
-
Save joerodgers/0266d1c017e7915dad40c7fc2c78f11b to your computer and use it in GitHub Desktop.
Gets files of a specific size then delete them
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
Add-PSSnapin Microsoft.SharePoint.Powershell | |
function Get-FileOfSize | |
{ | |
[CmdletBinding()] | |
param | |
( | |
[parameter(Mandatory=$true)][string]$SiteUrl, | |
[parameter(Mandatory=$true)][int]$Size | |
) | |
begin | |
{ | |
} | |
process | |
{ | |
$site = Get-SPSite -Identity $SiteUrl | |
foreach( $web in $site.AllWebs ) | |
{ | |
foreach( $library in $web.GetListsOfType([Microsoft.SharePoint.SPBaseType]::DocumentLibrary) ) | |
{ | |
if( -not $library.Hidden ) | |
{ | |
foreach( $item in $library.Items ) | |
{ | |
if( $item.File.Length -eq $Size ) | |
{ | |
[PSCustomObject] @{ | |
Site = $site.Url | |
Web = $web.Url | |
List = $library.Title | |
FileUrl = $site.MakeFullUrl($item.File.Url) | |
Size = $item.File.Length | |
Author = $item.File.Author | |
Created = $item.File.TimeCreated | |
} | |
} | |
} | |
} | |
} | |
$web.Dispose() | |
} | |
$site.Dispose() | |
} | |
end | |
{ | |
} | |
} | |
function Remove-File | |
{ | |
[CmdletBinding()] | |
param | |
( | |
[parameter(Mandatory=$true)][string]$FileUrl | |
) | |
begin | |
{ | |
} | |
process | |
{ | |
$site = New-Object Microsoft.SharePoint.SPSite($FileUrl) | |
$web = $site.OpenWeb() | |
$file = $web.GetFile($FileUrl) | |
if( $file.Exists ) | |
{ | |
#$file.Recycle() | |
$file.Delete() | |
} | |
else | |
{ | |
Write-Error "File Not Found: $FileUrl" | |
} | |
$web.Dispose() | |
$site.Dispose() | |
} | |
end | |
{ | |
} | |
} | |
$files = Get-FileOfSize -SiteUrl "https://sharepoint.2016.contoso.com/sites/teamsite" -Size 0 | |
$files | Export-Csv -Path "Files.csv" -NoTypeInformation | |
foreach( $file in $files ) | |
{ | |
Remove-File -FileUrl $file.FileUrl | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment