How to generate a GUID in VBScript?
Question
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
OTHER TIPS
Function CreateGUID
Dim TypeLib
Set TypeLib = CreateObject("Scriptlet.TypeLib")
CreateGUID = Mid(TypeLib.Guid, 2, 36)
End Function
This function will return a plain GUID, e.g., 47BC69BD-06A5-4617-B730-B644DBCD40A9
.
If you want a GUID in a registry format, e.g., {47BC69BD-06A5-4617-B730-B644DBCD40A9}
, change the function's last line to
CreateGUID = Left(TypeLib.Guid, 38)
' Returns a unique Guid on every call. Removes any cruft.
Function CreateGuid()
CreateGuid = Left(CreateObject("Scriptlet.TypeLib").Guid,38)
End Function
Set tlib = Server.CreateObject("Scriptlet.TypeLib")
strGuid = tlib.Guid
You Can Gen Random GUID Via This Object => "Scriptlet.TypeLib"
Help Link => With...End
Here is My Way =
With CreateObject("Scriptlet.TypeLib")
'' For Del This = {}
WSH.Echo mid(.Guid ,2, 36)
WSH.Echo (.Guid)
End With