Search code examples
vbscriptguiduuid

How to generate a GUID in VBScript?


I want to generate GUID strings in VBScript. I know that there's no built-in function in VBScript for generating one. I don't want to use random-generated GUIDs. Maybe there is an ActiveX object that can be created using CreateObject() that is sure to be installed on (newer) Windows versions that can generate a GUID?


Solution

  • How Can I Create a GUID Using a Script? (in: Hey, Scripting Guy! Blog) says this:

    Set TypeLib = CreateObject("Scriptlet.TypeLib")
    Wscript.Echo TypeLib.Guid
    

    However, note that Scriptlet.TypeLib.Guid returns a null-terminated string, which can cause some things to ignore everything after the GUID. To fix that, you might need to use:

    Set TypeLib = CreateObject("Scriptlet.TypeLib")
    myGuid = TypeLib.Guid
    myGuid = Left(myGuid, Len(myGuid)-2)
    Wscript.Echo myGuid