SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a PowerShell script creating new site collections, each containing a root SPWeb, on my SharePoint web application.

I have a THMX file that I'm trying to apply to each SPWeb that the script creates, but I'm not sure how best to do this.

  • I can use ThmxTheme.Open(site, URL) to get a handle to the theme once it is uploaded.
  • I can use ThmxTheme.ApplyTo(web) to apply the theme to the SPWeb once I have the handle to it.
  • However, I can't figure out how to upload the theme in the script.

In the past, I've scripted the uploading of images to a site's Asset Library, for example, but I can't find the equivalent of SPWeb.Lists.EnsureSiteAssetsLibrary() to get a handle to the Theme Gallery.

Can anyone tell me

  1. how to get the THMX file into the theme gallery, and
  2. if there's an easier way to script setting the theme to an SPWeb than that bulleted above (e.g. if there's a way to get a handle to the theme without needing a server-relative URL)?
share|improve this question
up vote 3 down vote accepted

I think the theme gallery is one of the default libaries created from SPRequest/COM (I can't see it in the hive) So you could use the same trick that site provisioning uses, wait looping until certain lists are available.

#create site collection
$filename = "c:\blah.thmx"
$bytes = [System.IO.File]::ReadAllBytes($filename);
$name = [System.IO.Path]::GetFileName($filename);
$list = $null
while($list -eq $null) {
    Start-Sleep 5 #wait for list to be created
    $list = (get-spweb "http://siteurl").Lists |? {$_.Title -eq "Theme Gallery"}
#upload theme
$list.RootFolder.Files.Add($name, $bytes);

And it looks like ApplyTo is the way to go as ThemingProcessor is internal

Though there seems to be a sequence issue to be careful of, but you should be okay in script

share|improve this answer
This work great, thanks. One more thing: I'd like $filename to be relative if possible (".\blah.thmx"), but doing this in my script looks for "C:\windows\system32\blah.thmx" rather than "C:\path\to\script\blah.thmx" in ReadAllBytes. Is there a way to fix this? – Rawling Apr 19 '11 at 8:11
Got it, Resolve-Path does the trick. Cheers again :) – Rawling Apr 19 '11 at 8:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.