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

Audit Web Part Locations

Through PowerShell, you can export a csv list of every single app and web part and the URLs where they exist. This is useful for keeping track of where apps are visible and who has access to them.

The script will prompt you for the URL. Enter the entire URL of the top site collection site. For example, http://sp2013/sites/company.

When it prompts you for the folder, just enter /.


Param([Parameter(Mandatory=$true)]
[String]
$Url,
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_ -PathType 'Container'})]
[String]
$folder,
[Parameter(Mandatory=$false)]
[String]
$WP
)

if ((gsnp Microsoft.SharePoint.Powershell -EA SilentlyContinue) -eq $null){
asnp Microsoft.SharePoint.Powershell -EA Stop
}

$filename = "WebPartsReport_" + (Get-Date).ToFileTimeUtc().ToString() + ".csv"
$filenamewp = "WebPartsReport_" + $WP.Replace(" ","-") + "_" + (Get-Date).ToFileTimeUtc().ToString() + ".csv"

$logfile = Join-Path $folder $filename
$logfilewp = Join-Path $folder $filenamewp

$urlArray = $Url.Split(",")

$header = "File Url, Web Part Title, Web Part Type, Visible"

ac $logfile $header
ac $logfilewp $header

$logfilecontrol = $null

foreach ($SPsite in $urlArray){
Get-SPSite $SPsite| % {

foreach ($web in $_.AllWebs){

if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)){

$library = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$pages = $library.PagesList

foreach ($file in $pages.Items){

$fileUrl = $web.Url + "/" + $file.File.Url

$manager = $file.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);

$webparts = $manager.webparts

$webparts | %{
ac $logfile "$fileUrl, $($_.DisplayTitle), $($_.GetType().ToString()), $($_.IsVisible)"

if ($_.DisplayTitle -match $WP -and -not [string]::IsNullOrEmpty($WP)){
ac $logfilewp "$fileUrl, $($_.DisplayTitle), $($_.GetType().ToString()), $($_.IsVisible)"

$logfilecontrol = 1
}
}
}

$sitepages = [Microsoft.Sharepoint.Utilities.SpUtility]::GetLocalizedString('$Resources:WikiLibDefaultTitle',"core",$web.UICulture.LCID)

$pages = $null
$pages = $web.Lists[$sitepages]

if ($pages -and $pages.ItemCount -gt 0){

foreach ($file in $pages.Items) {
$fileUrl = $web.Url + "/" + $file.File.Url

$manager = $file.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);

$webparts = $manager.webparts

$webparts | %{
ac $logfile "$fileUrl, $($_.DisplayTitle), $($_.GetType().ToString()), $($_.IsVisible)"

if ($_.DisplayTitle -match $WP -and -not [string]::IsNullOrEmpty($WP)){
ac $logfilewp "$fileUrl, $($_.DisplayTitle), $($_.GetType().ToString()), $($_.IsVisible)"

$logfilecontrol = 1
}
}
}
}
} else {
$sitepages = [Microsoft.Sharepoint.Utilities.SpUtility]::GetLocalizedString('$Resources:WikiLibDefaultTitle',"core",$web.UICulture.LCID)

$pages = $null
$pages = $web.Lists[$sitepages]

if ($pages){

foreach ($file in $pages.Items) {
$fileUrl = $web.Url + "/" + $file.File.Url

$manager = $file.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);

$webparts = $manager.webparts

$webparts | %{
ac $logfile "$fileUrl, $($_.DisplayTitle), $($_.GetType().ToString()), $($_.IsVisible)"

if ($_.DisplayTitle -match $WP -and -not [string]::IsNullOrEmpty($WP)){
ac $logfilewp "$fileUrl, $($_.DisplayTitle), $($_.GetType().ToString()), $($_.IsVisible)"

$logfilecontrol = 1
}
}
}
}
}
}
}
}

.\notepad.exe $logfile

if ($logfilecontrol -eq 1){
.\notepad.exe $logfilewp
}

Update List/Library Columns With External Content Type Columns

Scenario:

You have a column on a custom list or library that needs to be back-filled with data from an External Content Type (ECT).

Solution:

Create a list from the ECT and use PowerShell to update your custom list’s column with the ECT’s list column.

Creating a list (assuming you already connected an ECT):

Open SharePoint Designer 2013 and navigate to your External Content Types. Click on the ECT you need data from and click “Create List & Form”.

Name the list whatever you want and click OK.

The list will be created in the site or subsite you’re connected to in SharePoint Designer.

Updating Custom List With ECT List Column:

My example shows how to match Customer names in an ECT with corresponding Folders in a document library to update the Status column.  If you need to update every item in a list, comment out line 78, un-comment lines 72 and 81, then remove the query.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Get the context - top level site collection
$ctx = Get-SPServiceContext http://sp2013

#Get the scope
$scope = new-object Microsoft.SharePoint.SPServiceContextScope $ctx

#Get the target site collection
$webTarget = Get-SPWeb -identity "http://sp2013/sites/customer"

#Get the ECT List
$list = $webTarget.Lists["Entity List"]

#Array to Hold Result - PSObjects
$ListItemCollection = @()

#Get All List items. Replace -name "Name" with the column name on the ECT list.
#Add more columns if required
$list.Items | foreach {
$ExportItem = New-Object PSObject
$ExportItem | Add-Member -MemberType NoteProperty -name "Entity" -value $_["Entity"]
$ExportItem | Add-Member -MemberType NoteProperty -name "Status" -value $_["Status"]

#Add the object with property to an Array
$ListItemCollection += $ExportItem
}

#Export the result Array to CSV file
$ListItemCollection | Export-CSV "E:\SharePoint\EntityStatus.csv" -NoTypeInformation

#Dispose the web Object
$webTarget.Dispose()

#Pause for 10 seconds for the CSV file to populate all the way.
Start-Sleep 10

function Update-SPList()
{
#Import to destination list
#This section of the PowerShell will loop through the csv file we created and update the Status column in our Customer Folders library.
$csvVariable= Import-CSV -path "E:\SharePoint\EntityStatus.csv"

# Destination site collection. You can use any site, it does not have to be in the same collection as ECT list.
$WebURL = "http://sp2013/sites/customer"

# Destination list name
$listName = "Customer Folders"

#Get the SPWeb object and save it to a variable
$webDestination = Get-SPWeb -identity $WebURL

#Query ONLY folders.
$query = New-Object Microsoft.SharePoint.SPQuery;
$query.Query =
" <Where>

<Eq>
<FieldRef Name='FSObjType' />
<Value Type='Integer'>1</Value>
</Eq>

</Where>";

$list = $webDestination.Lists[$listName]

#If you want to update all items, remove the query and uncomment the following line
#$items = $list.Items

#loop through csv file
foreach($row in $csvVariable)
{
#Updating queried items
$updateitem = $list.GetItems($query) | Where {$_["Title"] -eq $row.Entity}

#Updating all items
#$updateitem = $items | Where {$_["Title"] -eq $row.Entity}

#loop through SharePoint list
Write-Host $row.Entity

$updateitem["Status"] = $row.Status
$updateitem.Update()
}

if ($webDestination)
{
$webDestination.Dispose()
}
}
Update-SPList

Update All List Items Using PowerShell

Scenario:

You added a new column and need to update that column for every item in a list or library.

Solution:

Here is an example PowerShell script that can be used to update one or more columns for every single item in a list.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

function UpdateColumn()
{
#URL where the list or library lives
$WebURL = "http://sp2013/subsite"

#name of that list or library
$listName = "Documents"

#get the SPWeb object and save it to a variable
$webDestination = Get-SPWeb -identity $WebURL
$list = $webDestination.Lists[$listName]

#array of all items
$items = $list.Items

foreach ($item in $items)
{
Write-Output $item["FileLeafRef"]

#change "Column" to internal name of your column
#change "Something" to the information you're updating that column with
$item["Column"] = "Something"
$item.Update()
}

if ($webDestination)
{
$webDestination.Dispose()
}
}

UpdateColumn

Remember: change $WebURL to the URL of the site or subsite where the list or library lives, change “Column” in $item[“Column”] to your column’s internal name, and change “Something” to whatever information you want to update that column with.

Write PowerShell Output To Text File

Here is an example of how I use the Write-Output command to log every update my PowerShell script made.


$items = $list.Items

$output = foreach ($item in $items)
{
Write-Host "Updating" $item["FileLeafRef"]
Write-Output "Updating" $item["FileLeafRef"]

$item["Status"] = "Active"
$item.Update()
}
$output | Out-File E:\SharePoint\PSUpdateStatus.txt

The $output variable needs to wrap the collection of items being updated (the foreach statement) if you want to combine all outputs into the same file. Then use the Write-Output function to state what needs to logged.

Unlock Documents With PowerShell

One of my users was constantly getting locked out of his own documents. Upon saving a document, he would receive the following error:

The file “filename” is locked for exclusive (or shared) use by “his username””

The issue was that his user only had “contribute” permissions. While that should be enough, he needed “edit” permissions to avoid locking his own documents.

While troubleshooting, I used a PowerShell script that unlocks documents that are locked for shared use.


Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

#Variables for Web and File URLs
$WebURL ="http://twainstlportal.twainfinancial.com/sites/crmdocs/BD"
$FileURL = "http://sp2013/sites/company/filename.docx"

#Get Web and File Objects
$web = Get-SPWeb $WebURL
$File = $web.GetFile($FileURL)

#Check if File is Checked-out
if ($File.CheckOutType -ne "None")
{
Write-host "File is Checked Out to user: " $File.CheckedOutByUser.LoginName
Write-host "Checked Out Type: " $File.CheckOutType
Write-host "Checked Out On: " $File.CheckedOutDate

#To release from checkout, ask the checked out user to check in
#$File.Checkin("Checked in by Administrator")
#Write-host "File has been Checked-In"
}

#Check if File is locked
if ($File.LockId -ne $null)
{
Write-host "File is Loked out by:" $File.LockedByUser.LoginName
Write-host "File Lock Type: "$file.LockType
Write-host "File Locked On: "$file.LockedDate
Write-host "File Lock Expires on: "$file.LockExpires

#To Release the lock, use:
#$File.ReleaseLock($File.LockId)
#Write-host "Released the lock!"
}

Change the filename to the URL of the locked document. Uncomment line 32 and 33 to unlock the document.

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()