-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGet-EmptyFolders.ps1
47 lines (43 loc) · 1.96 KB
/
Get-EmptyFolders.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<#
.SYNOPSIS
Get empty folders.
.DESCRIPTION
Get the empty folders from a specific SharePoint Online site.
.EXAMPLE
PS C:\> .\Get-EmptyFolders.ps1
This script will retrieve all the empty folders from a specific site collection in SharePoint Online
.INPUTS
Inputs (if any)
.OUTPUTS
Output (if any)
.NOTES
More info on my blog post: https://veronicageek.com/office-365/sharepoint-online/find-empty-folders-in-a-sharepoint-site-using-powershell-pnp/2020/02/
#>
#Connect to SPO
Connect-PnPOnline -Url "https://<YOUR_TENANT_NAME>.sharepoint.com/sites/<YOUR_SITE>"
#Store in variable all the document libraries in the site
$DocLibrary = Get-PnPList | Where-Object { $_.BaseTemplate -eq 101 }
$LogFile = "C:\users\$env:USERNAME\Desktop\SPOEmptyFolders.csv"
$results = @()
foreach ($DocLib in $DocLibrary) {
#Get list of all folders in the document library
$AllItems = Get-PnPListItem -PageSize 1000 -List $DocLib -Fields "SMTotalFileStreamSize", "Author"
#Loop through each files/folders in the document library for >50Mb
foreach ($Item in $AllItems) {
if ((([uint64]$Item["SMTotalFileStreamSize"]) -eq 0)) {
Write-Host "Empty folder:" $Item["FileLeafRef"] -ForegroundColor Yellow
#Creating new object to export in .csv file
$results += [pscustomobject][ordered] @{
CreatedDate = [DateTime]$Item["Created_x0020_Date"]
FileName = $Item["FileLeafRef"]
CreatedBy = $Item.FieldValues.Author.LookupValue
FilePath = $Item["FileRef"]
SizeInMB = ($Item["SMTotalFileStreamSize"] / 1MB).ToString("N")
LastModifiedBy = $Item.FieldValues.Editor.LookupValue
EditorEmail = $Item.FieldValues.Editor.Email
LastModifiedDate = [DateTime]$Item["Modified"]
}
}#end of IF statement
}
}
$results | Export-Csv -Path $LogFile -NoTypeInformation