Step by Step – Using C# Code in the PowerShell 2.0 Scripts

Have you ever had C# functions library and you wanted to reuse it in the PowerShell script instead of reinventing the wheels and translating the c# code into PowerShell. Thanks to the PowerShell 2.0’s new Add-Type Cmdlet demonstrated by the Stefen that lets you define a .NET Framework class in your PowerShell session. For more info on the PowerShell Add-Type command, visit the technet link to get familiar with various ways of sharing the C# code, assemblies into the PowerShell scripts. There are many more ways you can integrate or reference the C# code libraries in the powershell. This is best way to reuse your existing code which would not only promote the code reuse but save the rewrite time.

In the previous blog entry, I had a C# code to update the ListFormWebPart’s TemplateName property on the Upload.aspx, EditForm.aspx, and DispForm.aspx pages of all the document libraries for all the sites in given site collection. Instead of translating the C# code into PowerShell, I wanted to reuse in the PowerShell as inline source code as follows.

To use the C# inline code in the PowerShell script, there are four specific steps.

#Step 1 – Assign the referenced assemblies into powershell variable

$RefAssem = (
“Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c”,
“System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”

#Step 2 – Assign the C# code block into powershell variable

$Source = @”
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls.WebParts;

namespace DocLib.Tools
public static class FolderUtility
public static void UpdateFolders(SPWeb spWeb, SPList spList)

// Update forms
foreach (SPForm spForm in spList.Forms)
if (spForm.Url.Contains(“DispForm.aspx”) || spForm.Url.Contains(“EditForm.aspx”) || spForm.Url.Contains(“Upload.aspx”))
string fileURL = spWeb.Url + “/” + spForm.Url;
SPFile page = spWeb.GetFile(fileURL);

using (SPLimitedWebPartManager lwpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
// Enable the Update
lwpm.Web.AllowUnsafeUpdates = true;

// Check out the file, if not checked out
SPFile file = lwpm.Web.GetFile(fileURL);
if (file.CheckOutType == SPFile.SPCheckOutType.None)

// Find the ListFormWebPart and Update the Template Name Property
foreach (System.Web.UI.WebControls.WebParts.WebPart wp in lwpm.WebParts)
if (wp is Microsoft.SharePoint.WebPartPages.ListFormWebPart)
Microsoft.SharePoint.WebPartPages.ListFormWebPart lfwp =
lfwp.TemplateName = “ListForm”;

// Update the file
file.CheckIn(“System Update”);

// Disable the Unsafe Update
lwpm.Web.AllowUnsafeUpdates = false;
if (lwpm.Web != null)
lwpm.Web.Dispose(); // SPLimitedWebPartManager.Web object Dispose() called manually


#Step 3 – Load the referenced assemblies and sourcecode in the powershell runtime environment using the Add-Type. Specify the langauage of the code library.

Add-Type -ReferencedAssemblies $RefAssem -TypeDefinition $Source -Language CSharp

#Step 4 – Call the C# methods from the powershell script.

[DocLib.Tools.FolderUtility]::UpdateFolders($spweb, $spList)

Hope this will save some precious time. 🙂

This entry was posted in Uncategorized. Bookmark the permalink.