How to assign to a nullable Guid in PowerShell? RRS feed

  • Question

  • Long ago, in a galaxy far far away there was a database with a table one of whose columns was a nullable uniqueidentifier.  That table got converted into type got thrown into an OData service got wrapped in a series of PowerShell commands.  Upshot of all this wonderful back story is that we have exposed some objects to PowerShell one of whose members is a variable of type Nullable<Guid> in C# parlance.  The problem comes when we want to assign a null value to this.  In C# it would be a matter of:

    Workspace w = new Workspace();
    w.WorkspaceCategoryId = null;

    In PowerShell, the simplistic point of view would be that it should be something like:

    $w = New-Workspace
    $w.WorkspaceCategoryId = $null

    Unfortunately what happens is: Exception setting "WorkspaceCategoryId": "Cannot convert null to type "System.Guid"."

    Just to be sure, I quickly checked the type of the variable with

    $w.PSObject.Properties | Where-Object { $_.Name -eq "WorkspaceCategoryId" }

    and the TypeNameOfValue comes back as System.Nullable`1[[System.Guid]]

    Despite many arcane incantations ($w.WorkspaceCategoryId = ([System.Nullable[[System.Guid]]]$null), ([System.Nullable[[System.Guid]]]$w.WorkspaceCategoryId) = $null, ([System.Nullable[[System.Guid]]$w.WorkspaceCategoryId) = ([System.Nullable[[System.Guid]]]$null, [System.Nullable[[System.Guid]]]$foo = $null; $w.WorkspaceCategoryId = $foo, etc.) there doesn't seem to be a way to assign a null value to this variable.  PowerShell seems to intercept anything heading to the variable and try to dereference it or convert it to a Guid before even trying to assign it, and $null seems to be an object as opposed to an actual value, meaning it doesn't get convert properly.  Am I missing something, or is there really no way to assign null to a nullable Guid variable in PowerShell?

    Tuesday, September 29, 2015 10:13 PM


All replies

  • PS C:\scripts> $x=[System.Nullable`1]
    PS C:\scripts> $x

    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Nullable`1                               System.ValueType


    Tuesday, September 29, 2015 10:25 PM
  • You might need to do this:



    Tuesday, September 29, 2015 10:29 PM
  • $foo = [System.Nullable]
    $w.WorkspaceCategoryId = $foo
    Exception setting "WorkspaceCategoryId": "Cannot convert the "System.Nullable" value of type "System.RuntimeType" to type "System.Guid"."

    Thursday, October 1, 2015 5:07 PM
  • I think this is pretty close to what I had in the examples I listed, but....

    [System.Nullable[[System.Guid]]]$nullableGuid = $NULL
    $w.WorkspaceCategoryId = $nullableGuid
    Exception setting "WorkspaceCategoryId": "Cannot convert null to type "System.Guid"."

    Thursday, October 1, 2015 5:13 PM
  • Please look closely a this:


    Note the characters that you have removed.


    • Proposed as answer by Walshy64 Wednesday, September 7, 2016 3:28 PM
    Thursday, October 1, 2015 5:46 PM
  • Here is how to generate an "Empty" (null) GUID:



    Thursday, October 1, 2015 5:49 PM
  • There might be a solution that involves using the psbase syntax. See:

    I'm not able to suggest exact code to exploit the idea for your object, but I wanted to float it by you.

    Thursday, October 1, 2015 10:27 PM
  • [System.Nullable``1[[System.Guid]]]$nullguid = $null
    $w.WorkspaceCategoryId = $nullguid
    Exception setting "WorkspaceCategoryId": "Cannot convert null to type "System.Guid"."
    System.Nullable is a well supported enough type that System.Nullable``1, System.Nullable or just straight Nullable work equally well.  Unfortunately, at this point, equally well is still not at all.
    Friday, October 2, 2015 4:27 PM
  • Yeah, that's an option in the long term.  I was hoping to be able to avoid having to write code to specially convert particular values to null.  Magic values are never a good thing.
    Friday, October 2, 2015 4:29 PM
  • That is exactly the trick I needed, yes!  Thank you!

    $w.psbase.WorkspaceCategoryId = $null

    Does what is needed.

    Friday, October 2, 2015 4:47 PM
  • That is exactly the trick I needed, yes!  Thank you!

    $w.psbase.WorkspaceCategoryId = $null

    Does what is needed.

    I first learned about psbase from the book "Pro Windows PowerShell"by Hristo Deshev.
    Friday, October 2, 2015 4:58 PM