Skip to content

Instantly share code, notes, and snippets.

@joerodgers
Last active October 14, 2019 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joerodgers/0266d1c017e7915dad40c7fc2c78f11b to your computer and use it in GitHub Desktop.
Save joerodgers/0266d1c017e7915dad40c7fc2c78f11b to your computer and use it in GitHub Desktop.
Gets files of a specific size then delete them
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