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)?

1 Answer 1


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


  • 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, 2011 at 8:11
  • Got it, Resolve-Path does the trick. Cheers again :)
    – Rawling
    Apr 19, 2011 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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