February 27th, 2008

Hey, Scripting Guy! How Can I Create a GUID Using Windows PowerShell?

Hey, Scripting Guy! Question

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?

— DL

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DL. Before we begin we’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!

Oh, wait, never mind. We’re actually planning on doing that this summer; it’s too cold to invade Canada at this time of year.

Note to our Canadian readers. Don’t worry too much about this. If you’ve ever seen Dean’s diet you’ll know that his reign won’t last very long anyway.

Instead, what we need for now are 158 volunteers to enter at least one event in the 2008 Winter Scripting Games. Why 158 volunteers? Well, if we can get 158 more people to enter, we’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.

Note. What’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’ve obviously never worked for Microsoft, have you?

Now, we know what you’re thinking. You’re thinking, “Enter the Scripting Games? Aren’t the Scripting Games over?” 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 one of those events (or just one of the Sudden Death Challenge events) and you’ll help us reach our new-found goal of doubling the size of the Games. And you’ll also be eligible to win one of the great prizes we’re giving away as part of the Scripting Games.

Best of all, and unless you sprain your finger while typing, you won’t even get hurt. Try to find another daring mission that can make that guarantee!

Tell you what. To sweeten the pot a little, put this phrase on your submission:

SUICIDE MISSION

We’ll see if we can set aside a couple bobbleheads to be randomly given away to those who volunteer for this mission.

Wait; check that. The Scripting Editor doesn’t like it when we arbitrarily decide to hand out prizes that we aren’t even sure that we have. Therefore, don’t put the phrase SUICIDE MISSION on your submission. Instead, use this code phrase:

UICIDE-SAY ISSION-MAY

She’ll never catch on to that.

Editor’s Note: The Scripting Editor not only caught on, but has just had a short discussion with the Scripting Guy who writes this column:

“What are you doing?”
“We need 158 more people.”
“First of all, I’d already changed ‘suicide mission’ to ‘daring mission,’ then saw you had it spelled out in all-caps – and pig latin – in middle of the article…”
“I really didn’t think you’d catch on…”
“Second…you want to reward people who haven’t shown up for the first 6 events?”
“We need 158 more people.”
“You’re hopeless.”

And yes, that last phrase is how the Scripting Editor ends most conversations with the Scripting Guy who writes this column.

Although…only 158 more….

Just to be on the safe side, though, you probably shouldn’t tell people that you’re part of the suicide (daring) mission. If anyone asks, just tell them you’re a humble system administration scripter who’s interested in generating GUIDs using Windows PowerShell. To make sure that no one can blow your cover, here’s a little script that does generate a GUID using Windows PowerShell:

$guid = [guid]::NewGuid()
$guid

As you can see, generating a GUID using Windows PowerShell is probably not the most difficult scripting chore you’ll ever face. (Boy, wouldn’t that be nice if it was the most difficult scripting chore you’ll ever face?) In line 1 we’re simply creating an instance of the .NET Framework class System.Guid, using PowerShell’s [guid] type adapter to reference the class name. We then call the static method NewGuid 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:

ba76b0c6-9f23-49c2-92ec-ce9adff7d51e

Scripting Guys trivia. By astonishing coincidence, our script generated a GUID that’s exactly the same as Scripting Guy Peter ba76b0c6-9f23-49c2-92ec-ce9adff7d51e Costantini’s middle name! What do you suppose the odds are of that happening?!?

Like we said, probably not the most difficult scripting chore you’ll ever face.

As long as we’re on the subject, and because we need to at least make it look like we put some effort into today’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’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 ToString method, will convert your GUID to its corresponding string value:

$guid = $guid.ToString()

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:

$guid = $guid.ToByteArray()

The resulting byte array is going to look a little something like this:

74
40
241
15
25
150
206
76
181
163
190
136
165
70
206
167

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?

That should do it, DL. We won’t keep you any longer today; we know that at least 158 of you are itching to take part in the suicide – uh, take part in the uicide-say ission-may. Just remember, there’s nothing to be afraid of: no one has ever gotten hurt in the Scripting Games, and pretty much everyone who’s ever participated has had a great time. Again, how many suicide (daring) missions can make that claim?

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 ….

Editor’s Note: 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?

Author

0 comments

Discussion are closed.