Upload a .STP Template File

Server Administration

This is a quick way to programmatically upload a .STP template file within SharePoint using PowerShell – useful if you’re transferring list templates from a development to a live environment.

$web = Get-SPWeb "http://mydomain"
$folder = $web.GetFolder( "List Template Gallery" )
$files = $folder.Files
$file = Get-ChildItem "C:\MyListTemplate.stp"
$newfile = $files.Add( "_catalogs/lt/MyListTemplate.stp", $file.OpenRead(), $true )
$web.Dispose()

PowerShell Snippets for Microsoft Exchange

Development

These are two very handy PowerShell snippets that I use again and again within our work environment.

Retreive List of All Group Members

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
csvde -f C:\report.csv -s mydomain.co.uk -r "memberof=CN=Dept:MyTartgetGroup,OU=Domain Groups,DC=mydomain,DC=ac,DC=uk" -l "sAMAccountName,name, mail"

Retreive List of All Users with Permission to Email a Group

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
Get-DistributionGroup "Dept:MyTargetGroup" | Select name,@{Name="AcceptMessagesOnlyFrom";Expression={[string]::join(";",($_.AcceptMessagesOnlyFrom | foreach {$_.Name}))}} | Export-Csv C:\report.csv

SharePoint & Item-Level Permissions within Document Libraries

Server Administration

I have a love/hate relationship with SharePoint. Often the things that should be really simple, just aren’t. Take the “Item-level Permissions” option for instance. Every list appears to have this option (available via List Settings > Advanced Settings) except for document libraries. I’m sure the SharePoint Development Team at Microsoft have a very good reason for this…

Step forth PowerShell!

$web = Get-SPWeb http://mysharepointsite
$list = $web.Lists[“A Document Library”]
$list.ReadSecurity = 2
$list.WriteSecurity = 2
$list.Update()
$web.Dispose()

The ReadSecurity value can be one of the following:

1 = “Read all items”
2 = “Read items that were created by the user”

The “Create and Edit access” option can also be set by specifiying a value for WriteSecurity.

1 = “Create and edit all items”
2 = “Create items and edit items that were created by the user”
4 = “None”

Getting a list of all files

Development

I do like PowerShell. Something that used to take 10-20 lines of VBS code, such as generating a report of all files stored within a folder, can now be reduced to a single line.

Get-ChildItem "X:\Software" -recurse | Select-Object Name, Extension, Length, CreationTime, LastWriteTime, Fullname | Export-CSV "D:\File Report.csv"

Hiding Fields in SharePoint 2010

Server Administration

This is a useful peice of PowerShell script to hide specific fields from the Edit form. ShowInEditForm can also be amended to ShowInNewForm.

$assignment = Start-SPAssignment
$web = Get-SPWeb http://mysharepointserver -AssignmentCollection $assignment
$field = $web.Lists["Announcements"].Fields["Body"]
$field.ShowInEditForm = $False
$field.Update()
Stop-SPAssignment $assignment