Tag Archives: Powershell

Unhide a hidden SPField using PowerShell

Another task that’s much harder than it should be. Setting the hidden property of a SPField to false.

First you get the field itself. In this example we change the setting of the field in the library, but it could just as well be done on the site level.

$web = Get-SPWeb http://MyWeb
$list = $web.Lists["MyList"]
$field = $list.Fields.GetFieldByInternalName("MyField")

Then, when you have the actual field, you just need a bit of reflection magic.

$type = $field.GetType()
$mi = $type.GetMethod("SetFieldBoolValue",[System.Reflection.BindingFlags]$([System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Instance))
$mi.Invoke($field, @("CanToggleHidden",$true))

I don’t know why, but simply setting field.Hidden = $false doesn’t work. But the code above does!

Solution comes from here.

How to reference nested class or struct etc, in PowerShell

I had a problem trying to reference a struct inside a public class from PowerShell. I tried to do it just like in C#, only with PowerShell syntax, simply adding the nested class or struct in the namespace, separated by a dot.

Doesn’t work:

$myProperty = [MyProject.MyClass.MyClassStruct]::MyProperty

But this didn’t work. Instead I got the following error:
Unable to find type [MyProject.MyClass.MyClassStruct]: make sure that the assembly containing this type is loaded.”

It seems, after fiddling around a bit and doing a quick search, that PowerShell has it’s very own way of calling a nested class or struct. Instead of adding the nested class with a dot, you actually have to use a +, like this:


$myProperty = [MyProject.MyClass+MyClassStruct]::MyProperty

I don’t know why, but that’s the way you do it. =)



Set boolean properties in powershell

I just wanted to make an existing list hidden without using SharePoint Designer, since I hate SPD. =)

So I wrote a simple powershell script to do this and quickly realized I didn’t know how to write boolean values. I tried $list.Hidden = true but it generated an error. I tried $list.Hidden = “True” but it changed nothing. A quick search on the net and I found the answer. $list.Hidden = $true worked. So boolean values are written with the dollar sign in front. Good to know. =)

So this is how to set boolean values in PowerShell:


Just in case someone wants to know. Here is the full example on how to hide a list using PowerShell.

$web = Get-SPWeb "http://url" 
$list = $web.Lists["list name"] 
$list.Hidden = $true 

Remove SharePoint feature witch name starts with… using powershell

This is how to search for features which display name starts with the given parameter (in this case it would list features which started with MyFeature).

Get-SPFeature | where { $_.DisplayName.StartsWith("MyFeature") }

EDIT: You can also find features using GUID instead with the following script:

Get-SPFeature | where { $_.Id.ToString().StartsWith("MyGUID") }

This will remove all those features which are found using the previous script.

Get-SPFeature | where { $_.DisplayName.StartsWith("MyFeature") } | foreach { Uninstall-SPFeature $_.Id -confirm:0 -force }

Sometimes (especially in SharePoint 2013), retracting wsps aren’t done properly (by SharePoint that is), and you might get errors when you are trying to reinstall a retracted wsp. After you have retracted a wsp, if the features in that wsp can still be found by Get-SPFeature in PowerShell, and/or you get an error message installing the wsp that “A feature with ID <guid> has already been installed in this farm.”, you might have to manually remove some of the resources yourself. These steps often work for me.

  1. Retract and remove wsp from server.
  2. Run powershell script above to check if the features of the wsp are still installed.
  3. If there are still features installet, delete them from the SPFarm manuall with the following command:
    $feats = (Get-SPFarm).FeatureDefinitions
    $f = $feats | where {$_.DisplayName.Contains("Feature name")} 

    This only works with single results, so make sure you get only one feature in variable $f Before deleting.

  4. Remove feature folders from the hive (for example C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\FEATURES”
  5. Remove any references in the assembly. In SP2010 (and earlier perhaps), the assembly is located at C:\Windows\assembly, and for SP2013, you can find it at C:\Windows\Microsoft.NET\assembly\GAC_MSIL.
  6. Restart the SharePoint Timer Service (Found in Services)
  7. Restart IIS (cmd “iisreset /noforce”)