{"id":56113,"date":"2008-02-27T23:18:00","date_gmt":"2008-02-27T23:18:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/02\/27\/hey-scripting-guy-how-can-i-create-a-guid-using-windows-powershell\/"},"modified":"2008-02-27T23:18:00","modified_gmt":"2008-02-27T23:18:00","slug":"hey-scripting-guy-how-can-i-create-a-guid-using-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-create-a-guid-using-windows-powershell\/","title":{"rendered":"Hey, Scripting Guy! How Can I Create a GUID Using Windows PowerShell?"},"content":{"rendered":"<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" class=\"nearGraphic\" \/><\/p>\n<p>Hey, Scripting Guy! I see that it is pretty easy to generate a GUID using <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/feb05\/hey0221.mspx\"><b>Windows Script Host<\/b><\/a>. But how can I do this using Windows PowerShell?<\/p>\n<p>&#8212; DL<\/p>\n<p><img decoding=\"async\" height=\"5\" width=\"5\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" alt=\"Spacer\" border=\"0\" \/><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" class=\"nearGraphic\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" height=\"288\" width=\"120\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" align=\"right\" alt=\"Script Center\" border=\"0\" title=\"Script Center\" class=\"farGraphic\" \/><\/a><\/p>\n<p>Hey, DL. Before we begin we&rsquo;d like to announce that the Scripting Guys are looking for 158 volunteers, volunteers to undertake a daring mission. What sort of mission? We are planning to overthrow the government of Canada and install Scripting Guy Dean Tsaltas as Supreme Dictator for Life!<\/p>\n<p>Oh, wait, never mind. We&rsquo;re actually planning on doing that this summer; it&rsquo;s too cold to invade Canada at this time of year.<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"EFD\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><b>Note to our Canadian readers<\/b>. Don&rsquo;t worry too much about this. If you&rsquo;ve ever seen Dean&rsquo;s diet you&rsquo;ll know that his reign won&rsquo;t last very long anyway. <\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Instead, what we need for now are 158 volunteers to enter at least one event in the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><b>2008 Winter Scripting Games<\/b><\/a>. Why 158 volunteers? Well, if we can get 158 more people to enter, we&rsquo;ll have exactly twice as many competitors as we had last year. Needless to say, doubling the size of the Games in just one year has a really nice ring to it.<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"EYD\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><b>Note<\/b>. What&rsquo;s that? If we double the size of the Scripting Games will Microsoft double our salaries as a reward for a job well done? You&rsquo;ve obviously never worked for Microsoft, have you?<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Now, we know what you&rsquo;re thinking. You&rsquo;re thinking, &ldquo;Enter the Scripting Games? Aren&rsquo;t the Scripting Games <i>over<\/i>?&rdquo; Au contraire! As it turns out, you can still enter Events 7, 8, 9, and 10 in the Games, as well as the daily events in the Sudden Death Challenge. Enter just <i>one<\/i> of those events (or just one of the Sudden Death Challenge events) and you&rsquo;ll help us reach our new-found goal of doubling the size of the Games. And you&rsquo;ll also be eligible to win one of the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08\/prizes.mspx\"><b>great prizes<\/b><\/a> we&rsquo;re giving away as part of the Scripting Games.<\/p>\n<p>Best of all, and unless you sprain your finger while typing, you won&rsquo;t even get hurt. Try to find another daring mission that can make <i>that<\/i> guarantee!<\/p>\n<p>Tell you what. To sweeten the pot a little, put this phrase on your submission:<\/p>\n<pre class=\"codeSample\">SUICIDE MISSION<\/pre>\n<p>We&rsquo;ll see if we can set aside a couple <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08\/prizes.mspx\"><b>bobbleheads<\/b><\/a> to be randomly given away to those who volunteer for this mission.<\/p>\n<p>Wait; check that. The Scripting Editor doesn&rsquo;t like it when we arbitrarily decide to hand out prizes that we aren&rsquo;t even sure that we have. Therefore, <i>don&rsquo;t<\/i> put the phrase SUICIDE MISSION on your submission. Instead, use this code phrase:<\/p>\n<pre class=\"codeSample\">UICIDE-SAY ISSION-MAY<\/pre>\n<p>She&rsquo;ll never catch on to <i>that<\/i>.<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"ELF\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><b>Ed<\/b><b>itor&rsquo;s Note:<\/b> The Scripting Editor not only caught on, but has just had a short discussion with the Scripting Guy who writes this column:<\/p>\n<p>&ldquo;What are you doing?&rdquo;<br \/>&ldquo;We need 158 more people.&rdquo;<br \/>&ldquo;First of all, I&rsquo;d already changed &lsquo;suicide mission&rsquo; to &lsquo;daring mission,&rsquo; then saw you had it spelled out in all-caps &ndash; and pig latin &ndash; in middle of the article&hellip;&rdquo;<br \/>&ldquo;I really didn&rsquo;t think you&rsquo;d catch on&hellip;&rdquo;<br \/>&ldquo;Second&hellip;you want to reward people who haven&rsquo;t shown up for the first 6 events?&rdquo;<br \/>&ldquo;We need 158 more people.&rdquo;<br \/>&ldquo;You&rsquo;re hopeless.&rdquo;<\/p>\n<p>And yes, that last phrase <i>is<\/i> how the Scripting Editor ends most conversations with the Scripting Guy who writes this column.<\/p>\n<p>Although&hellip;only 158 more&hellip;.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Just to be on the safe side, though, you probably shouldn&rsquo;t tell people that you&rsquo;re part of the suicide <i>(daring)<\/i> mission. If anyone asks, just tell them you&rsquo;re a humble system administration scripter who&rsquo;s interested in generating GUIDs using Windows PowerShell. To make sure that no one can blow your cover, here&rsquo;s a little script that <i>does<\/i> generate a GUID using Windows PowerShell:<\/p>\n<pre class=\"codeSample\">$guid = [guid]::NewGuid()\n$guid<\/pre>\n<p>As you can see, generating a GUID using Windows PowerShell is probably not the most difficult scripting chore you&rsquo;ll ever face. (Boy, wouldn&rsquo;t that be nice if it <i>was<\/i> the most difficult scripting chore you&rsquo;ll ever face?) In line 1 we&rsquo;re simply creating an instance of the .NET Framework class System.Guid, using PowerShell&rsquo;s <b>[guid]<\/b> type adapter to reference the class name. We then call the static method <b>NewGuid<\/b> which, not too surprisingly, generates a new GUID and stores that value in a variable named $guid. In line 2, we then echo back the value of $guid:<\/p>\n<pre class=\"codeSample\">ba76b0c6-9f23-49c2-92ec-ce9adff7d51e<\/pre>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"EFH\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><b>Scripting Guys trivia<\/b>. By astonishing coincidence, our script generated a GUID that&rsquo;s exactly the same as Scripting Guy Peter ba76b0c6-9f23-49c2-92ec-ce9adff7d51e Costantini&rsquo;s middle name! What do you suppose the odds are of <i>that<\/i> happening?!?<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Like we said, probably not the most difficult scripting chore you&rsquo;ll ever face.<\/p>\n<p>As long as we&rsquo;re on the subject, and because we need to at least make it <i>look<\/i> like we put some effort into today&rsquo;s column, here are a couple of other GUID-related tasks you might find useful. When you call the NewGuid method you get back an instance of the System.Guid class. For the most part, that&rsquo;s great. In some cases, however, you might find it easier to work with a string representation of your GUID. If so, this line of code, and the <b>ToString<\/b> method, will convert your GUID to its corresponding string value:<\/p>\n<pre class=\"codeSample\">$guid = $guid.ToString()<\/pre>\n<p>There might also be times when you find it handy (maybe even necessary) to convert this new GUID to a byte array. (This sometimes occurs when working with Active Directory.) This line of code converts your GUID to a byte array:<\/p>\n<pre class=\"codeSample\">$guid = $guid.ToByteArray()<\/pre>\n<p>The resulting byte array is going to look a little something like this:<\/p>\n<pre class=\"codeSample\"><pre class=\"codeSample\">74\n40\n241\n15\n25\n150\n206\n76\n181\n163\n190\n136\n165\n70\n206\n167\n<\/pre>\n<p>Whoa. Add those numbers up, and you get the exact age, in years, of Peter Costantini, the oldest living Scripting Guy. Is that spooky or what?<\/p>\n<p>That should do it, DL. We won&rsquo;t keep you any longer today; we know that at least 158 of you are itching to take part in the suicide &ndash; uh, take part in the uicide-say ission-may. Just remember, there&rsquo;s nothing to be afraid of: no one has <i>ever<\/i> gotten hurt in the Scripting Games, and pretty much everyone who&rsquo;s ever participated has had a great time. Again, how many suicide <i>(daring)<\/i> missions can make <i>that<\/i> claim?<\/p>\n<p>As for those of you interested in the mission to overthrow the government of Canada, well, all we can say is this: be patient. Just be patient &hellip;.<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"EPAAC\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><b>Ed<\/b><b>itor&rsquo;s Note:<\/b> If anyone feels the need to lock away the Scripting Guy who writes this column for security reasons, well, who are we to stand in your way?<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I see that it is pretty easy to generate a GUID using Windows Script Host. But how can I do this using Windows PowerShell? &#8212; DL Hey, DL. Before we begin we&rsquo;d like to announce that the Scripting Guys are looking for 158 volunteers, volunteers to undertake a daring mission. What sort [&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":[3,4,45,77],"class_list":["post-56113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-writing"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I see that it is pretty easy to generate a GUID using Windows Script Host. But how can I do this using Windows PowerShell? &#8212; DL Hey, DL. Before we begin we&rsquo;d like to announce that the Scripting Guys are looking for 158 volunteers, volunteers to undertake a daring mission. What sort [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56113","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=56113"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56113\/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=56113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=56113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=56113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}