Mass Delete Empty Folders With PowerShell

If you don’t feel like manually deleting empty folders, use this PowerShell script to query and delete all empty folders and subfolders. Make sure you change the $WebURL to your SharePoint site or subsite, and the $listName to your library.


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

function EmptyFolders()
{
$WebURL = "http://sp2013/sites/company"
$webDestination = Get-SPWeb -identity $WebURL
$listName = "Documents"

$query = New-Object Microsoft.SharePoint.SPQuery;
$query.ViewAttributes = "Scope='RecursiveAll'";
$query.RowLimit = 5000
$caml = "<Where>
<Eq>
<FieldRef Name='ContentType' />
<Value Type='Text'>Folder</Value>
</Eq>
</Where>"

$query.Query = $caml

$list = $webDestination.Lists[$listName]
$folder = $list.GetItems($query)
do
{
$query.ListItemCollectionPosition = $folder.ListItemCollectionPosition
for ($index = $folder.Count - 1; $index -gt -1; $index--)
{
if ($folder[$index]["FolderChildCount"].Replace(';#', '') -eq 0 -and $folder[$index]["ItemChildCount"].Replace(';#', '') -eq 0)
{
Write-Host("$($folder[$index]["FolderChildCount"].Replace(';#', '')), $($folder[$index]["ItemChildCount"].Replace(';#', '')), $($folder[$index]["ContentType"]), $($folder[$index].URL)")
$folder[$index].Delete();
$list.Update()
}
}
}
While($query.ListItemCollectionPosition -ne $null)
#$list.Update();
$webDestination.Dispose()
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Operation Completed",0,"Done",0x1)
}
EmptyFolders

Hide Libraries From File Explore

Sometimes your users don’t need to see every single SharePoint library when they’re in file explorer. Here is a bit of PowerShell that will hide necessary libraries from file explorer. Make sure you change the URL to your SharePoint site’s URL and the folder name to the library you want to hide.

#folder can be site, library level and folder level
$folder = (Get-SPWeb http://sp2013/sites/company).Folders["Documents"]

#use this to hide subfolders
#$folder = (Get-SPWeb http://sp2013/sites/company).Folders["Documents"].SubFolders["MyDocs"]

#hide folder
$folder.Properties["vti_winfileattribs"]="00000016"

#show folder
#$folder.Properties["vti_winfileattribs"]=""

$folder.Update()