Share via


converting a string to [GUID]

Question

Friday, July 24, 2009 9:42 AM

Hi chaps,

[GUID]$a = "00000000-00000000-0000-000000000000"

doesnt work, although i didnt really expect it to...

 [Guid]::NewGuid($a) doesnt work either, but i must be close ? :)

All replies (5)

Friday, July 24, 2009 1:15 PM ✅Answered | 4 votes

You where very close ;)

PS C:\ $guid = [GUID]"00000000-0000-0000-0000-000000000000"
PS C:\ $guid.ToString()
00000000-0000-0000-0000-000000000000

actualy you allready did it (only number count was not correct )

PS C:\ [GUID]$a = "00000000-0000-0000-0000-000000000000"
PS C:\ $a

 

PS C:\ $a.tostring()
00000000-0000-0000-0000-000000000000

Note that the default output is not string version of the GUID you need to call the tostring method for that

Greetings /\\o\\


Friday, July 24, 2009 10:05 AM

I'm struggling with the concept of *converting* something to a GUID...  Why?

Why not just create a new one?

PS>[system.guid]::newguid()


Friday, July 24, 2009 10:14 AM

I am trying to preserve the value of 'VirtualSystemIdentifiers' on some Hyper-V WMI resources when 'cloning' a VM on another host.  I want to be able to do something like this:

 

 

 

$OLD_NIC = Get-WmiObject -namespace root/virtualization -query "SELECT * FROM Msvm_SyntheticEthernetPortSettingData WHERE ElementName = '$($vm) Network Adapter'"

[GUID]$GUID = $OLD_NIC.VirtualSystemIdentifiers

blah blah blah 

$new_nic.VirtualSystemIdentifiers = “{$GUID}”

 


Friday, July 24, 2009 1:38 PM

Ah, I know where I was going wrong. I'm an idiot :)

 

VirtualSystemIdentifiers of Msvm_SyntheticEthernetPortSettingData gives the GUID between closed and open 'squiggly brackets', i.e { and }, I'm not sure of their correct term. If I get the value of VirtualSystemIdentifiers as a [string] and then .replace("{",""), etc etc

It works fine.

 

Thanks guys!


Friday, July 24, 2009 1:50 PM | 1 vote

Another tip
instead of the replace you can also use the Trim method and get both  { and } removed at the same time :

"{4c935adb-d8a1-4e98-a837-f5f4904ab01a}".trim("{}")

4c935adb-d8a1-4e98-a837-f5f4904ab01a

Greetings  /\\o\\