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

3 Answers 3


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

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.


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

Your Answer

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

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