Configure SharePoint Blocked File Types using PowerShell


I’m working on building a PowerShell Script Repository using SharePoint 2010 as the source system. During this work, I’ve obviously had to enable the .ps1 file type for my SharePoint Web Application. As I was doing this manually, I thought “Come on Ryan, surely you can do that with PowerShell!”. Of course I was able to do so, and it was very simple. I found a few examples online also during my quick search, but most were not leveraging the OOTB SP Cmdlets, so I’m posting my versions which use the Cmdlets which are included with SharePoint 2010.

I ended up creating 3 functions to handle the Get, Add and Remove operations. They’re all very similar, they literally just get the WebApplication object and then either return the list of BlockedFileExtensions or use the Add() or Remove() methods to perform the operations.

I’ll post all 3 functions including comment-based help, enjoy!

Get-SPBlockedFileType


function Get-SPBlockedFileType {
<#
.Synopsis
 This function can be used to retrieve blocked file types for a SharePoint Web Application.
.Description
 This PowerShell function uses the SharePoint Object Model to get all file
 types in the BlockedFileExtensions property for a Web Application.
.Example
 C:\PS>Get-SPBlockedFileType -WebApplication http://intranet
 This example gets blocked file types for a SharePoint Web Application at http://intranet.
.Notes
 Name: Get-SPBlockedFileType
 Author: Ryan Dennis
 Last Edit: 6/14/2012
 Keywords: Get-SPBlockedFileType,Add-SPBlockedFileType,Remove-SPBlockedFileType
.Link
 http://www.sharepointryan.com
 http://twitter.com/SharePointRyan
.Inputs
 None
.Outputs
 None
#Requires -Version 2.0
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]$WebApplication
)
$WebApp = Get-SPWebApplication $WebApplication
$Extensions = $WebApp.BlockedFileExtensions
$Extensions | ForEach-Object {Write-Host $_}
}

Add-SPBlockedFileType


function Add-SPBlockedFileType {
<#
.Synopsis
 This function can be used to add a blocked file type for a SharePoint Web Application.
.Description
 This PowerShell function uses the SharePoint Object Model to add file
 types to the BlockedFileExtensions property for a Web Application.
.Example
 C:\PS>Add-SPBlockedFileType -WebApplication http://intranet -Extension ps1
 This example adds the ps1 file type to the BlockedFileExtensions collection
 for a SharePoint Web Application at http://intranet.
.Notes
 Name: Add-SPBlockedFileType
 Author: Ryan Dennis
 Last Edit: 6/14/2012
 Keywords: Get-SPBlockedFileType,Add-SPBlockedFileType,Remove-SPBlockedFileType
.Link
 http://www.sharepointryan.com
 http://twitter.com/SharePointRyan
.Inputs
 None
.Outputs
 None
#Requires -Version 2.0
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]$WebApplication,
[Parameter(Mandatory=$true)][System.String]$Extension
)
$WebApp = Get-SPWebApplication $WebApplication
$Extensions = $WebApp.BlockedFileExtensions
$Extensions.Add($Extension)
$WebApp.Update()
}

Remove-SPBlockedFileType


function Remove-SPBlockedFileType {
<#
.Synopsis
 This function can be used to remove a blocked file type from a SharePoint Web Application.
.Description
 This PowerShell function uses the SharePoint Object Model to remove file
 types from the BlockedFileExtensions property for a Web Application.
.Example
 C:\PS>Remove-SPBlockedFileType -WebApplication http://intranet -Extension ps1
 This example removes the ps1 file type from the BlockedFileExtensions collection
 of a SharePoint Web Application at http://intranet.
.Notes
 Name: Remove-SPBlockedFileType
 Author: Ryan Dennis
 Last Edit: 6/14/2012
 Keywords: Get-SPBlockedFileType,Add-SPBlockedFileType,Remove-SPBlockedFileType
.Link
 http://www.sharepointryan.com
 http://twitter.com/SharePointRyan
.Inputs
 None
.Outputs
 None
#Requires -Version 2.0
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]$WebApplication,
[Parameter(Mandatory=$true)][System.String]$Extension
)
$WebApp = Get-SPWebApplication $WebApplication
$Extensions = $WebApp.BlockedFileExtensions
$Ext = $Extensions.Remove($Extension)
if($Ext -eq $true){
Write-Warning "Filetype $($Extension) has been removed from Web Application $(($WebApp).Name)"
}
else{
Write-Warning "Unable to delete filetype $($Extension) from Web Application $(($WebApp).Name)"
}
$WebApp.Update()
}
Advertisements

4 thoughts on “Configure SharePoint Blocked File Types using PowerShell

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s