PowerShell error re mismatched object types

I'm trying to hide a specific template associated with a site.  So I have written some Powershell which gets the array of templates available to that web, copies the array to a new array while excluding the one I need to hide, and then sets the web to use the new array.  Like this:

$web = "http://my.server/web"

$existing = $web.GetAvailableWebTemplates(1033)

$new = @()
foreach($e in $existing | Where-Object { $_.name ne $template_2_hide})
{
$new += $e
}

$web.SetAvailableWebTemplates($new, 1033)

The final line is giving me an error which presumably means that the new array of objects is of the wrong type:

Cannot convert argument "0", with value: "System.Object[]", for "SetAvailableWebTemplates" to type "System.Collections.ObjectModel.Collection`1[Microsoft.SharePoint.SPWebTemplate]": "Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Collections.ObjectModel.Collection`1[Microsoft.SharePoint.SPWebTemplate]".

Can anyone help me please?  Thanks!

May 27th, 2015 3:47pm

Hi 

I think you can modify your script like below-

$web = Get-SPWeb http://my.server/web
$existing = @($web.GetAvailableWebTemplates(1033))
$new = $existing | where {$_.Name -ne $template2hide}
$web.SetAvailableWebTemplates($new, 1033)
$web.Update()
Hope it helps.

Free Windows Admin Tool Kit Click here and download it now
May 28th, 2015 1:51am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics