Generate Guids with PowerShell or ScriptCs
There are many ways to generate GUIDs. You can use one of the many free websites or you can write your own (quick) script to do it. If you want to create your own script, there are 3 easy approaches:
- PowerShell
- ScriptCs
- Uuidgen.exe
The first two are very powerful and versatile and can do a lot more than just generating GUIDs. The 3rd one is a dedicated utility for generating GUIDs only.
PowerShell
PowerShell is first as it's the easiest of the 2. Easiest to get started, that is. PowerShell is already installed on your Windows machine. Just bring up the command line and type PowerShell and it will take you to PowerShell mode (PS>
). Alternatively, you can use the PowerShell ISE which is the GUI equivalent and has some neat features like debugging etc. Finally, there's a number of 3rd party PowerShell editors you can install, but if this is going to be a quick and easy task, we'll ignore the last option.
I wrote a small function that takes the number of guids you want to create and an output file. The whole thing is less than 10 lines of scripting:
ScriptCs
ScriptCs is equally as versatile as PowerShell, but requires a bit of a setup first. The easiest way to get ScriptCs on your machine is through ChocolateyNuget:
cinst scriptcs
After the installation, you can start writing code with the power of C# and Roslyn. You can find more information and tutorials about ScriptCs on the [official ScriptCS website](http://scriptcs.net/" target="_blank). There are 2 ways to execute ScriptCs code:
- Type in the command line one line of code at a time (normal C# code, properly escaped)
- Put all your code in a *.csx file and executed it from the command line like this:
C:\ > scriptcs yourFileName.csx
For the purpose of this post we'll go with option 2 as it's easier to display the required code when it's all located in one place. Create a new <yourFileName>.csx:
and paste the following code:
Next, open the command prompt and type scriptcs yourfilename
. At the end of execution, you should find this file "c:\temp\scriptcsGuids.txt" in the C:\temp folder. This file should now contain 10 guids.
Uuidgen
Apparently, after I blogged this post, this happened:
@ChristosMatskas @khellang nice. You forgot to mention uuidgen ;-)
— 007 (@gblock) October 15, 2015
Totally unbeknown to me, there's another little utility available for generating GUIDs on windows from the command line: uuidgen.exe. You can find more info on this little gem [here](https://msdn.microsoft.com/en-us/library/aa373928(VS.85).aspx" target="_blank). And I will like to thank [Glenn Block](https://twitter.com/gblock" target="_blank) for guiding my path :)
Open the VS Developer Prompt and type this command:
uuidgen -oc:\temp\test.txt -n20
This command will generate 20 GUIDs and will store them in C:\temp\test.txt
Feel free to change the parameters based on your needs. The bottom line is that no matter which of the 2 options you choose, it's extremely easy to execute small, tedious tasks and make them fun using some really cool tools. ScriptCs and PowerShell are both amazing and I enjoy using to make things easy and fun. Do you turn to scripting for similar tasks in your day to day job?