24 December, 2012

5 PowerShell snippets for SharePoint branders

A very good post by Christian on PowerSheel Snippets.
PowerShell is a command-line scripting tool that provides an administrator full access to applicable application programming interfaces (APIs), along with the ability to unlock the capability to interact directly with SharePoint 2010 Products to manipulate Web applications, site collections, sites, lists and much more. So what’s a useful thing to do with PowerShell as a SharePoint brander or a front end developer? Well, there are a lot of different types of tasks you can use PowerShell to, for example populate a list with a large amount of objects when you need to performance test your Data View Web Part or if you need to apply a theme to let’s say some site in the structure including its sub sites or if you need to batch update properties of a collection page layouts.
Maybe you see something that can be written more efficient or if you have some cool snippets of your own you like to share, please drop this in a comment. Take these snippets as they are, try them first in your developer or test environment and feel free to use them and modify in the way you want.
Running PowerShell scripts is easy and you will find many resources out there if you never used PowerShell before, when you know the basics it’s just to go ahead and try these out. This stuff is written for SharePoint 2010 but most of this can be used in SharePoint 2013 as it is.
1. Change Master Page for all sites in the collection
In this example, we are apply a customer master to all the sites in the site collection, this can be rewritten specific for SharePoint foundation, where PowerShell is much useful for such task. In this snippet I have set V4.master, so have to change the name of the master page file if apply a custom master page.
# ----- For publishing sites and non publishing sites
$site = Get-SPSite http://intranet
foreach ($web in $site.AllWebs) {
$web; $web.CustomMasterUrl = "/_catalogs/masterpage/V4.master";
$web.Update(); $web.CustomMasterUrl;
$web.Dispose()
}
foreach ($web in $site.AllWebs) {
$web; $web.MasterUrl = "/_catalogs/masterpage/v4.master";
$web.Update(); $web.MasterUrl;
$web.Dispose()
}
$site.Dispose()
write-host "Complete! V4.master is now applied";
2. Set alternate CSS
In this way you can set a custom CSS file as an alternate CSS at the top site of your site collection, publishing sub sites will inherit the alternate CSS by default.
$web = Get-SPWeb http://intranet
$web.AlternateCssUrl = "/Style Library/MyStyles/main.css"
$web.AllProperties["__InheritsAlternateCssUrl"] = $True
$web.Update()
3. Set a site logo
With this one you can associate a logo with all sites in the site collection by entering the URL to an image file.
(get-spsite http://intranet).AllWebs | foreach {
$_.SiteLogoUrl = "/Style%20Library/MyClient/Images/ClientLogo.png";
4. Set regional setting/locale
This one can be handy when you need to specify the way the site displays numbers, dates, and time. In this example I set locale to Swedish (1053).
$site = Get-SPSite http://intranet
foreach ($web in $site.AllWebs) {
$web; $web.Locale = 1053;
$web.Update(); $web.Locale;
$web.Dispose()
}
$site.Dispose()
5. Set a theme
This one will set a theme to all sites in the site collection. This script is originally written by MVP Yaroslav Pentsarskyy
$SiteUrl = "http://intranet"
$NewTheme = "Azure"
# Loading Microsoft.SharePoint.PowerShell
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null) {
Write-Host "Loading SharePoint Powershell Snapin"
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}
# Setting site themes on sites and sub sites
$SPSite = Get-SPSite | Where-Object {$_.Url -eq $SiteUrl}
if($SPSite -ne $null)
{
$themes = [Microsoft.SharePoint.Utilities.ThmxTheme]::GetManagedThemes($SiteUrl);
foreach ($theme in $themes)
{
if ($theme.Name -eq $NewTheme)
{
break;
}
}
foreach ($SPWeb in $SPSite.AllWebs)
{
$theme.ApplyTo($SPWeb, $true);
Write-Host "Set" $NewTheme "at :" $SPWeb.Title "(" $SPWeb.Url ")"
}
}
Write-Host "Themes updated at:" $SPSite.Url -foregroundcolor Green
More
If you’re a truly SharePoint Designer guy, why not use PowerShell to check if you allowed to do your branding stuff before fire it up…
Get-SPDesignerSettings -webapplication http://intranet
There’s lot of blogs about using PowerShell in SharePoint but here’s a pic of a few cool scripts that somehow are related to this topic.
Finally he’d like to give thanks to MVP Cathy Dew for inspiring discussions about this topic; PowerShell from a brander’s perspective as well as he’d like to thanks Falak Mahmood for general discussions and suggestions for the use of PowerShell.


8 comments:

  1. Sweet blog! I found it while browsing on Yahoo News.

    Do you have any tips on how to get listed in Yahoo News?
    I've been trying for a while but I never seem to
    get there! Appreciate it

    Here is my web site - dead rising 2 Cheats xbox 360

    ReplyDelete
  2. Wow, this paragraph is fastidious, my younger sister is analyzing these things, thus I am going to
    convey her.

    Also visit my blog :: COC Tool

    ReplyDelete
  3. Very nice post. I just stumbled upon your weblog and wished to say that I
    have really enjoyed browsing your blog posts. In any case
    I will be subscribing to your rss feed and I hope you write again soon!


    My webpage :: ninja kingdom hack ()

    ReplyDelete
  4. Ԝhat's up to all, how is evеrүthіng, I thіnҡ
    eveгy onne is getting more from this web ѕite, and yokur
    views are gоod inn suppߋrt of new viewers.

    my ѡeblog :: art lawyer new york

    ReplyDelete
  5. I wɑs ablee to fiոd good iոformatіoո from үour articles.



    my blog; tacoma family lawyer

    ReplyDelete
  6. When Ӏ iոityially commenteɗ I seem to have clicked on
    the -Notify me wheո new comments are added-
    checkbox and now whenever ɑ comment is added I get 4 emails
    with the same comment. Is there a means yоu cаn remove me from that serviсe?

    Many thanks!

    my blog - real housewives of orange county

    ReplyDelete
  7. An electronic cigarette store maʏ be just wɦat iѕ nеeded.
    * Watch - Јust like іn the spy films where thе hero or
    heroine whips up ɦis watch, focus օn tҺе suspect or criminal
    mastermind аnd take а photograph օr video photographs,
    tҺese spy cameras disguiised аs watches are great tools tօ have for ɑny
    surveillance աork. Sinсe from thе paѕt fouг
    monnths I am using green smoke electronic cigarettes.

    my site; acquistare sigaretta elettronica

    ReplyDelete
  8. It's nearly impossible to find knowledgeable people for this topic, but you sound like you know what you're
    talking about! Thanks

    My site - pre workout energy

    ReplyDelete

Your feedback is always appreciated. I will try to reply to your queries as soon as possible- Amol Ghuge

Note: Only a member of this blog may post a comment.