I found a technet blog saying you could run c# code in powershell, so I ended up giving it a try. When I try to run the following powershell script I get 2 errors and I'm not sure how to resolve them. Does anyone know the fix for the errors below?

Technet Blog: Using CSharp (C#) code in Powershell scripts

Add-PSSnapin Microsoft.SharePoint.PowerShell

$Assem = {
    "Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c"

$Source = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace TurnAlertsOnOff
    class onOff
        static void Main(string[] args)
            #code goes here.


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


Error 1

Add-Type : Illegal characters in path.
At C:\Users\testUser\Desktop\test.ps1:115 char:1
+ Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-Type], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.AddTypeCommand

Error 2 (Not caused by first error.)

Unable to find type [TurnAlertsOnOff.onOff]: make sure that the assembly containing this type is loaded.
At C:\Users\testUser\Desktop\test.ps1:117 char:1
+ [TurnAlertsOnOff.onOff]::Main()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (TurnAlertsOnOff.onOff:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound
  • Can you include a link to the blog?
    – AdamBT
    Sep 5, 2014 at 15:07
  • Added the link.
    – zingwing
    Sep 5, 2014 at 15:10

1 Answer 1


When I've ran C# within PowerShell, I've done the assembly bit differently (I'm in O365, so only use CSOM):

$path = "path to folder containing dll's (e.g.,ISAPI folder in 15 Hive)"
$assemblies = @(

$cSharp = @"
using System;
using System.Management.Automation;
using System.Security;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Utilities;

namespace SPClient
    public class SharePointPS
        public static void Main()
            // Code here

Add-Type -TypeDefinition $cSharp -ReferencedAssemblies $assemblies

Never had any issues with it. System.Management.Automation and System.Security were used for handling the O365 authentication, so they are not necessary for you.

  • Thanks I changed the structure of my assembly and Add-Type and it resolved the first error. Now I need to figure out way I'm getting the second error.
    – zingwing
    Sep 5, 2014 at 15:52
  • 1
    Try specifing the class and function as public class/method
    – wjervis
    Sep 5, 2014 at 15:57
  • So the path would be: $path = "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI"
    – Underverse
    Mar 17, 2016 at 0:56
  • @Underverse Yes, that should be the path.
    – wjervis
    Mar 17, 2016 at 15:16

Your Answer

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

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