Download content from a site collection

I’ve been working on a script that will allow you to download all files that are stored in SharePoint in a given site collection.

If the path does not exist, the script will prompt you to create it for you. Before the script runs, it also checks if the site collection exists.

Run the script like this:

.\Get-SPContent.ps1 -SiteCollection "<SiteCollectionURL>" -Destination "<Path>"

Download content

The console shows which libraries were exported to your file system.

—– * Advanced * —–

If you have specific requirements as to which (type of) libraries you want to export, you can change the following line to fit your requirements:

$lists = $web.lists | ?{$_.itemcount -ge &quot;1&quot; -And $_.Hidden -eq $false -And $_.BaseType -eq &quot;DocumentLibrary&quot;} #Excludes all hidden libraries and empty libraries

Below is the code you can save as Get-SPContent.ps1

param
(
[Parameter(Mandatory=$true)]
[ValidateScript({asnp *sh* -EA SilentlyContinue;if (Get-SPSite $_){$true}else{Throw &quot;Site collection $_ does not exist&quot;}})]
[string]$SiteCollection,
[Parameter(Mandatory=$true)]
[ValidateScript(
{
if (Test-Path $_)
{$true}
else{
$d = $_
$title = &quot;Create Folder?&quot;;
$message = &quot;$_ doesn't exist, do you want the script to create it?&quot;;
$yes = New-Object System.Management.Automation.Host.ChoiceDescription &quot;&amp;Yes&quot;, &quot;Creates directory $_&quot;;
$no = New-Object System.Management.Automation.Host.ChoiceDescription &quot;&amp;No&quot;, &quot;Exits script&quot;;
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no);
$result = $host.ui.PromptForChoice($title,$message,$options,1);
switch($result)
{
0 {New-Item $d -Type Directory;$true}
1 {Throw &quot;Please create the folder before running the script again. `nExiting script&quot;}
}
}
})]
[string]$Destination
)

Asnp *sh* -EA SilentlyContinue

Start-SPAssignment -Global | Out-Null

function Get-SPWebs($SiteCollection){
$SiteCollection = Get-SPSite $SiteCollection
$webs = @()
$SiteCollection.allwebs | %{$webs += $_.url}
return $webs
}

function Get-SPFolders($webs)
{
foreach($web in $webs)
{
$web = Get-SPWeb $web
Write-Host &quot;`n$($web.url)&quot;

$lists = $web.lists | ?{$_.itemcount -ge &quot;1&quot; -And $_.Hidden -eq $false -And $_.BaseType -eq &quot;DocumentLibrary&quot;} #Excludes all hidden libraries and empty libraries
#$lists = $web.lists | ?{$_.title -eq &quot;Documents&quot; -and $_.itemcount -ge &quot;1&quot; -And $_.BaseType -eq &quot;DocumentLibrary&quot;} #Change any identifier here
foreach($list in $lists)
{
Write-Host &quot;- $($list.RootFolder.url)&quot;

#Download files in root folder
$rootfolder = $web.GetFolder($list.RootFolder.Url)
Download-SPContent($rootfolder)

#Download files in subfolders
foreach($folder in $list.folders)
{
$folder = $web.GetFolder($folder.url)
Download-SPContent($folder)

}

}
$web.dispose()
}
}

function Download-SPContent($folder)
{
foreach($file in $folder.Files)
{
$binary = $file.OpenBinary()
$stream = New-Object System.IO.FileStream($destination + &quot;/&quot; + $file.Name), Create
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.write($binary)
$stream.Close()
$writer.Close()
}
}

$webs = Get-SPWebs -SiteCollection $Sitecollection
Get-SPFolders -Webs $webs

Stop-SPAssignment -Global

Add PDF mimetype for all Web Applications oneliner

By default, PDF files cannot be opened directly from SharePoint 2010/SharePoint 2013.

To add the PDF mimetype to all Web Applications (Instead of doing it seperately for each Web Application), you can use the following oneliner:

Get-SPWebApplication | %{$_.AllowedInlineDownloadedMimeTypes.Add("application/pdf");$_.Update()}

Get all subsites of a subsite using PowerShell

Getting a list of all subsites of a particular site (not a site collection) was a little more work than I expected, so here is how I did it.

Let’s say we have the following situation site structure:

SiteStructure

What if we want an overview of all sites under “Https://portal.sharepointrelated.com/Projects”?

My first thought was to use the “Webs” property of the SPWeb object. Unfortunately, this only shows the direct subsites for this site. This means that for “Https://portal.sharepointrelated.com/projects”, it only shows the Level 3 sites.

Solution

To work around this, I used the “AllWebs” property of the SPSite object and filtered the URL’s starting with “Https://portal.sharepointrelated.com/projects”.

Here is the code used: (Download .zip file)


param ( [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] [String]$StartWeb, [Boolean]$IncludeStartWeb = $true )

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$subsites = ((Get-SPWeb $StartWeb).Site).allwebs | ?{$_.url -like "$StartWeb*"}

foreach($subsite in $subsites) { Write-Host $subsite.url }

As you can see in the source code, I added 2 parameters to the script:

StartWeb: String. This is the starting URL. All subsites under this site will be showed in the result.

IncludeStartWeb: Boolean. When set to $false, the output will not include the URL provided in the StartWeb parameter.

Remove users from User Information List (people picker) using PowerShell

This post will describe how you can remove users in bulk from the User Information List using a PowerShell script and a simple CSV file.

In SharePoint 2010, the people picker retrieves data from multiple sources.

– The Site Collection’s User Information List (UIL);
– Active Directory.

When you delete a user from Active Directory, this will not mean the user isn’t searchable in SharePoint. Actually, if you look for this person in the people picker, you will probably find him/her. As the data is pulled from different sources, there may be several causes for this.

Assuming the user is really deleted from the Active Directory, I will give you some pointers as to how to “delete” the users from the People picker.

Scenario
I have a user called Kim Akers in my Contoso environment. She has permissions on several sites/subsites, and placed documents and list items in multiple places.

Example site

She also has a MySite.

Profile

For some reason, Kim is fired. The Active Directory administrators remove her account from Active Directory.

However, when I look at the People Picker in SharePoint, I can still find this user.

People picker

Why is this happening?

Every user that is given direct permissions, or has logged in to SharePoint, will be added to the Site Collection’s User Information List. This is a hidden list, that you can access by going to your site collection’s URL and add /_catalogs/users/simple.aspx. For instance: http://portal.contoso.com/_catalogs/users/simple.aspx.

This will show a list of all users that have logged in on your SharePoint. Sure enough, Kim can still be found here, even though her account has been deleted in Active Directory.

User Information List

Solution

To remove the user from the information list, you can use the GUI. If you want more information on how to do this, read this article. Also, make sure the profile for this user is not in the Profile Database. You can remove users from the Profile Database directly by going to Central Administration -&> Application Management -&> Manage Service Application -&> Click your User Profile Service Application -&> Manage User Profiles -> Find profile by entering the name -> Select the name in the list, and click Delete.

In my case, I wanted to remove a list of users from All site collections, because I am certain that these users will never log in again, and I don’t want them to show in the people picker. The below script will do just that!

(
[Parameter(Mandatory=$true)][ValidateScript({Test-Path $_ -Include "*.csv"})]
[String]$CSVPath
)

#This script will remove users specified in the CSV.

$CSVFile = Import-CSV $CSVPath
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue

#Get all site collections
$Sites = Get-SPSite -Limit All
$AllSites = @()
foreach($Line in $CSVFile)
{
foreach($Site in $Sites)
{
#Get the rootweb for the site collection
$RootWeb = $Site.RootWeb
If([bool](Get-SPUser $Line.Username -Web $RootWeb -EA SilentlyContinue) -eq $True)
{
#Remove the user from the User Information List
Remove-SPUser -Identity $Line.username -Web $RootWeb -Confirm:$False
$AllSites += $RootWeb.Url
}
}
if(!($AllSites).count -eq 0)
{
#Give feedback on deleted users
Write-Host "Removed user $($Line.username) from:" -Fore "Magenta"
foreach($S in $AllSites){Write-Host "- $S"}
Write-Host ""
$AllSites = @()
}
}

I save the above text in a .ps1 file called Remove-SPUserBulk.ps1.

Next, I create a CSV file (Users.csv) that will contain all the users that I want to remove. My demo CSV looks like this:

CSV

As you can see, I added a non-existing account, to show that the script actually just deletes the existing user, and the output is correct. I run the script by going to the location where the Remove-SPUserBulk.ps1 file is located, and enter: “Remove-SPUSerBulk.ps1 -CSVPath “C:scriptsUsers.csv”.

Below is the result.

Script

Be aware that if the user is a site collection administrator, you will get an error stating you cannot delete the owners of a Web site collection.

Site collection creation time using PowerShell

This script will show you the site collection creation time for each site collection using PowerShell.
Try the following oneliner to get all the information you need:

Get-SPSite -Limit All | %{$root = $_.rootweb; $date = $root.created.toShortDateString(); Write-Host "$($root.url) was created on $($date)"}

Site collection creation time

Change site collection administrators using PowerShell

In a minute, I will show you how to change the site collection administrators for all site collections using PowerShell. Thanks to Maarten Peeters for helping!

But first, it may be easy to get a result for all the current site collection administators, in case you want to roll back:

Get-SPSite -Limit all | FT Url, Owner, SecondaryContact -AutoSize

Site collection administrators

Now if you want to change all site collection administrators and secondary site collection administrators (As you would set in Central Administration)

Get-SPSite -Limit All | %{Set-SPSite $_ -OwnerAlias "<domainuser>" -SecondaryOwnerAlias "<domainuser>"}

If you are using MySites and personal sites have been created, you might want to use the following, to exclude the MySite site collections. Please check if you are using the “personal” managed path. If you are using a different managed path, you can change the “*/personal/*” to reflect your environment.

Get-SPSite -Limit All | ?{$_.url -notlike "*/personal/*"} | %{Set-SPSite $_ -OwnerAlias "<domainuser>" -SecondaryOwnerAlias "<domainuser>"}

I hope this helps someone out there to save some valuable time!

Maximum file size per extension in SharePoint 2013

Summary: This blog post describes how to use a new feature to set a maximum file size per extension in SharePoint 2013.

As most of you know, you are able to set the maximum upload size in SharePoint 2010 and SharePoint 2013 in the General settings for each Web Application.
The default maximum upload size for SharePoint 2013 is 250MB.

Maximum upload size

Maximum file size per file extension
A new cool feature that I really like in the preview is the possibility to add exceptions for certain file types.
By default, the .one extension has a maximum upload size of 1024MB (1GB)

Now what if we want to add another exception. Let’s say I want to allow .wmv files up to 1GB as well.
We can use PowerShell to add this extension. So far, I have not found a GUI interface to do the same:

$WebApp = Get-SPWebApplication "http://portal.sharepointrelated.com"
$WebApp.MaximumFileSizePerExtension.Add("wmv",1024)
$WebApp.Update()

To see all current maximum file sizes per extension:

$WebApp = Get-SPWebApplication "http://portal.sharepointrelated.com"
$WebApp.MaximumFileSizePerExtension


Current limits

Removing a maximum file size for an extension

$WebApp.MaximumFileSizePerExtension.Remove("wmv")
$WebApp.Update()

This will remove the wmv again, so the maximum allowed upload size for .wmv files will be 250MB again.

New maximum upload limit

Update Secure Store Service Application using PowerShell

This post describes how you can update the Secure Store Service Application using PowerShell.

You can use the following script to update information for one of your Secure Store Applications.
I want to change the Contact E-mail property for this Secure Store Application to “NewEmail@xxx.com”

Used Configuration
For this part, I have set up the following:

– 1 Web Application (My intranet)
– 1 Secure Store Service Application
– Created 1 Application: “TestID”:

Secure Store Service Application

Pre-requisites
I found that before this works, you need the following:
– Site Collection Administrator for the root of the web application (my intranet)
– Full Control on the Secure Store Service Application. You can set this by going to:
Central Administration -> Application Management -> Manage Service Application -> Highlight the Secure Store Service Application -> Select “Administrators” from the ribbon.

Secure Store administrators

Changing the desired property

#Add the PowerShell Snapin
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue #Retrieve the Service context
$site = new-object Microsoft.SharePoint.SPSite("https://demo.contoso.com");
$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);

#Get the Secure Store Application and save the new information into a variable
$Application = Get-SPSecureStoreApplication -Name "TestID" -ServiceContext $ServiceContext
$TargetApplication = $Application.TargetApplication
$TargetApplication.ContactEmail = "NewEmail@xxx.com"

#Set the saved information to the Target Application
Set-SPSecureStoreApplication -Identity $Application -TargetApplication $TargetApplication

Result

Target Application ID

Content Database FAQ

This post describes the most frequently asked questions on a content database in SharePoint.

Since there is a lot of misconception on how different SharePoint components relate to each other, I will try to answer some common questions. Please feel free to comment or ask additional questions in the comment section. I will add them to the FAQ list.

The below figure shows the infrastructure of Content databases related to Web Applications and site collections.

Content Database FAQ

FAQ

1. Can we create a site collection in a specific Content Database?
Yes, but you can only do this by using PowerShell using the New-SPSite cmdlet. You can use the “-ContentDatabase” parameter to specify in which Content Database the site collection will be created.

2. When using the “-ContentDatabase” parameter in the New-SPSite cmdlet, does the content database get automatically created if it does not exist?
No, if you enter a Content Database name that does not exist, you will get an error saying the content database could not be found.

3. My site collection is getting very big, can I add a second Content Database to spread the load?
Because of the way SharePoint is designed, you cannot have 1 site collection spread over multiple Content Databases. If you want to split up your site collection in to multiple site collections, you can do this by exporting and importing subsites, or by using 3rd party tools.

4. What site collections are currently in my Content Database?
For this specific question, I have written a PowerShell one-liner that will return every Content Database with their respective site collections. You can find this one liner here: https://www.sharepointrelated.com/2012/03/09/get-site-collections-per-content-database-one-liner/

5. How can I check the size of my Content Database?
The easiest way to accomplish this is by looking at the database file on your SQL Server. But there may be some scenario’s where this is not possible. Insufficient permissions being the most common one. In this case, you can use this article to find out the approximate Content Database size: https://www.sharepointrelated.com/2012/03/07/content-database-size-report-using-powershell/

6. What Content Databases are attached to my Web Applications?
For this question, I have also written a small blog post. It will return all Web Applications with their respective Content Databases: https://www.sharepointrelated.com/2012/05/28/get-content-databases-per-web-application-one-liner/

7. Why are my Content Databases named WSS_Content_<Guid>?
The moment you create a Web Application, you will be asked to fill in details regarding your content database. By default, SharePoint will fill in this field for you with the following syntax: WSS_Content. If a content database with this name already exists, it will add a GUID to it.

8. What if I want to move my site collection to another Content Database?
In this case, there are 2 scenario’s:
– Move the site collection to another Content Database that is attached to the same Web Application
You can use the Move-SPSite cmdlet to move the site collection.
– Move the site collection to another Content Database that is attached to another Web Application
You can use the Backup-SPSite and Restore-SPSite functionality to accomplish this task.

9. I have multiple site collections in my Content Database, can I use PowerShell to see their sizes?
Yes, you can. You can use the following line of code to check the approximate size of your site collection (From Todd’s blog: Using PowerShell to find site collection size in SharePoint 2010)

Get-SPSite "http://someurl.com" | select url, @{label="Size in MB";Expression={$_.usage.storage/1MB}}

10. Is it possible to maintain the same Content Database for more than one Web Application?
No, this is not possible. A Content Database can only be attached to a single Web Application.