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 want to create a PowerShell script that :

  1. add a farm solution that only contains a site template
  2. deploy the solution globally
  3. wait for the deploy job to finish
  4. create a site collection based on the site template

The first three steps are working as expected. The 4th does not. I get the following error :

New-SPSite : File or arguments not valid for site template 'myTemplate#0'.
Parameter name: WebTemplate
At D:\SCRIPT\install.ps1:73 char:11
+ New-SPSite <<<<  $url -OwnerAlias "$user"  -Name "my site name" -Template mytemplate#0" -Language 1036
    + CategoryInfo          : InvalidData: (Microsoft.Share...SPCmdletNewSite:SPCmdletNewSite) [New-SPSite], ArgumentException
    + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletNewSite

If I create the site collection from the central administration, it works perfectly.

I suspect there is a "cache" of available templates somewhere, but did not found neither the source, nor the solution.

Here is my full script :

Add-PSSnapin Microsoft.SharePoint.PowerShell

Start-SPAssignment -Global

function WaitForJobToFinish([string]$SolutionFileName)
    $JobName = "*solution-deployment*$SolutionFileName*"
    $job = Get-SPTimerJob | ?{ $_.Name -like $JobName }
    if ($job -eq $null)     {        Write-Host 'Timer job not found'    }
    else    {
        $JobFullName = $job.Name
        Write-Host -NoNewLine "Waiting to finish job $JobFullName"

        while ((Get-SPTimerJob $JobFullName) -ne $null) {
            Write-Host -NoNewLine .
            Start-Sleep -Seconds 2
        Write-Host  "Finished waiting for job.."

$user = (get-item env:userdomain).Value +'\' + (get-item env:username).Value
$webapp = 'http://myvirtualhost'
$url = 'http://myvirtualhost/sites/mysite'
$solutionName = 'mytemplate.wsp'

Add-SPSolution -LiteralPath (get-item $solutionName).Fullname

Install-SPSolution -Identity $solutionName -GACDeployment

WaitForJobToFinish $solutionName

remove-spsite $url -Confirm:$false 
iisreset /noforce # trying to recycle the cache without success
New-SPSite $url -OwnerAlias "$user"  -Name "my site name" -Template "mytemplate#0" -Language 1036

Stop-SPAssignment -Global
share|improve this question
up vote 4 down vote accepted

You are right about that PowerShell caches alot.

You can get around this by spawning new processes that executes the code

For example

$shell = Start-Process Powershell .\test.ps1 -PassThru -Verb runas

will spawn a new process that is run as administrator (-Verb runas) and traces the process (-PassThru) giving you the possibility to track exit codes and if process has finished. Check out Start-Process syntax using

Get-Help Start-Process -full

Check out members of Process using

$shell | Get-Member
share|improve this answer

Please find the script that I used in the following post at the end of Section B:

$file = "D:\Rahul\RestoreWSPSiteTemplate.log"

start-transcript $file
trap { stop-transcript; break}
Remove-SPSite -Identity "http://DEST_SERVER_NAME:PORT_NO/sites/ExampleRestore"
New-SPSite "http:// DEST_SERVER_NAME:PORT_NO/sites/ExampleRestore" -OwnerAlias domain\rahul.vartak 

Add-SPUserSolution -LiteralPath "D:\Rahul\ExampleTemplate.wsp" -Site "http:// DEST_SERVER_NAME:PORT_NO/sites/ExampleRestore"

Install-SPUserSolution –Identity ExampleTemplate.wsp -Site "http:// DEST_SERVER_NAME:PORT_NO/sites/ExampleRestore"

$x = Get-SPSite "http:// DEST_SERVER_NAME:PORT_NO/sites/ExampleRestore"
$y = $x.GetWebTemplates(1033) | Where{ $_.Title -eq "ExampleTemplate" }

$site= new-Object Microsoft.SharePoint.SPSite("http:// DEST_SERVER_NAME:PORT_NO/sites/ExampleRestore")

Note: This assumes that you are on the destination server while running the script.

share|improve this answer

I ended up by using the legacy stsadm -o createsite. It is a lot simplier that having to create process or ps-session. It's actually a clean powershell solution, but it works

share|improve this answer

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.