{"id":70393,"date":"2005-02-21T08:55:00","date_gmt":"2005-02-21T08:55:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/02\/21\/how-can-i-create-a-guid-using-a-script\/"},"modified":"2005-02-21T08:55:00","modified_gmt":"2005-02-21T08:55:00","slug":"how-can-i-create-a-guid-using-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-create-a-guid-using-a-script\/","title":{"rendered":"How Can I Create a GUID Using a Script?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/> <\/p>\n<p>Hey, Scripting Guy! We have items that we need to track using unique identification numbers. My boss suggested that we use GUIDs for this purpose. Is there any way to create a GUID using a script?<\/p>\n<p>&#8212; DX<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\" \/><\/a> <\/p>\n<p>Hey, DX. GUIDs (globally unique identifiers) are a good choice for a job like this; we don\u2019t pretend to understand the algorithm behind GUIDs, but you are all-but-guaranteed that every GUID you generate will be unique. Although it\u2019s theoretically possible to generate a duplicate GUID, it\u2019s also theoretically possible that Bill Gates will give all his money to the Scripting Guys. We aren\u2019t going to lose any sleep over either possibility.<\/p>\n<p>There\u2019s actually a very easy way to generate GUIDs, although it almost seems like cheating. (Hey, we said <i>almost<\/i>.) The <b>Scriptlet.TypeLib<\/b> object is designed to help you create Windows Script Components (basically a way of taking a script you wrote and making it act like a COM object). The Scriptlet.Typelib object includes a method that generates GUIDs for use with Windows Script Components type libraries; however, there\u2019s no reason you can\u2019t use this method to generate a GUID for other purposes. (After all, a GUID is a GUID is a GUID.) If you need a GUID, here\u2019s a two-line script that will supply you with one:<\/p>\n<pre class=\"codeSample\">Set TypeLib = CreateObject(\"Scriptlet.TypeLib\")\nWscript.Echo TypeLib.Guid\n<\/pre>\n<p>Not bad, huh? Of course, this script merely echoes the GUID back in a message box; if you want to actually use that GUID somewhere else, you\u2019ll have to type it in. That might be OK if it wasn\u2019t for the fact that GUIDs look like this:<\/p>\n<pre class=\"codeSample\">{24DD18D4-C902-497F-A64B-28B2FA741661}\n<\/pre>\n<p>And so, being the generous, self-serving guys that we are, we modified the script a bit. This script generates a GUID but then, instead of displaying that GUID in a message box, copies the GUID to the Clipboard:<\/p>\n<pre class=\"codeSample\">Set TypeLib = CreateObject(\"Scriptlet.TypeLib\")\nstrGUID = TypeLib.Guid\n\nSet objIE = CreateObject(\"InternetExplorer.Application\")\nobjIE.Navigate(\"about:blank\")\nobjIE.document.parentwindow.clipboardData.SetData \"text\", strGUID\nobjIE.Quit\n<\/pre>\n<p>If you\u2019re wondering how we do that, well, we\u2019re cheating once again. VBScript can\u2019t copy items to the Clipboard, but Internet Explorer can. So we\u2019re simply generating a GUID and then storing that value in a variable named strGUID. We then create an invisible instance of Internet Explorer, and use the <b>clipboardData.SetData<\/b> method to copy that GUID to the clipboard. That\u2019s what we do in this line of code:<\/p>\n<pre class=\"codeSample\">objIE.document.parentwindow.clipboardData.SetData \"text\", strGUID\n<\/pre>\n<p>We quit Internet Explorer, and now are free to go paste that GUID in anywhere we want to use it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! We have items that we need to track using unique identification numbers. My boss suggested that we use GUIDs for this purpose. Is there any way to create a GUID using a script? &#8212; DX Hey, DX. GUIDs (globally unique identifiers) are a good choice for a job like this; we don\u2019t [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[17,3,4,5,77],"class_list":["post-70393","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-internet-explorer","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-writing"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! We have items that we need to track using unique identification numbers. My boss suggested that we use GUIDs for this purpose. Is there any way to create a GUID using a script? &#8212; DX Hey, DX. GUIDs (globally unique identifiers) are a good choice for a job like this; we don\u2019t [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70393"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70393\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}