dotps1 dotps1 - 9 months ago 87
PowerShell Question

Convert ObjectGuid from AD to GUID powershell

I cannot understand why this does not work in PowerShell v4:

New-Object -TypeName System.Guid((New-Object -TypeName DirectoryServices.DirectorySearcher -Property @{ Filter = "(&(objectCategory=computer)(name=$env:COMPUTERNAME))" }).FindOne().Properties.Item('objectguid')[0])

when this works in PowerShell v5:

[Guid]::New((New-Object -TypeName DirectoryServices.DirectorySearcher -Property @{ Filter = "(&(objectCategory=computer)(name=$env:COMPUTERNAME))" }).FindOne().Properties.Item('ObjectGuid')[0])

But unfortantly the 'New' method does not exist in PowerShell v4. But, the first example should work. the arg is a Byte Array, as the it should accept: GUID MSDN. But i get an error that says:

Cannot find an overload for "Guid" and the argument count: "16"

Any help is much appreciated. Thanks!


I got it to work, must have been using the wrong constructor? i don't know but this works:

New-Object -TypeName System.Guid -ArgumentList @(,(New-Object -TypeName DirectoryServices.DirectorySearcher -Property @{ Filter = "(&(objectCategory=computer)(name=$env:COMPUTERNAME))" }).FindOne().Properties.Item('objectguid')[0])

Hope it helps someone, sometime, somewhere.