Posted on:
Categories: PowerShell
Description:

Scenario

You are using PowerShell to execute a Method on a C# Object. This Method has a string parameter which you must pass a null string into. All of the usual options did not work, $null, [String]::Empty, [object]$null, [string]$null…

Solution

      $Assem = ("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
$cSharp = @"
    using System;

 namespace PSHelper
 {
  public class NullString
  {
   private readonly static NullString _value;

   public static NullString Value
   {
    get
    {
     return NullString._value;
    }
   }

   static NullString()
   {
    NullString._value = new NullString();
   }

   private NullString()
   {
   }

   public override string ToString()
   {
    return null;
   }
  }
 }
"@
Add-Type -ReferencedAssemblies $Assem -TypeDefinition $cSharp -Language CSharp;

[PSHelper.NullString]::Value