How can I generate a GUID in a batch file running using the commandline in Windows?
The Windows SDK comes with a tool called uuidgen
(if you have Visual Studio, you'll have the Windows SDK, and you need to run the Visual Studio Command Prompt to set proper paths).
C:\>uuidgen
This will output a new GUID, e.g.
cc23b318-156e-473f-aa6e-517bf091a0f0
PATH
for me. Also, it doesn't seem to be really random but time-based (Unix versions of this utility have a -r
and -t
flags to specify if you want it random-based or time-based. –
Schuster Try this if you have powershell environment.
FOR /F %a IN ('POWERSHELL -COMMAND "$([guid]::NewGuid().ToString())"') DO ( SET NEWGUID=%a )
Then ready Guid value from %NEWGUID%
.bat
), don't forget to double the %
in the FOR
statement (so the line should read FOR /F %%a IN ('POWERSHELL -COMMAND "$([guid]::NewGuid().ToString())"') DO ( SET NEWGUID=%%a )
) –
Nightlong easy to do in powershell
[guid]::NewGuid()
New-Guid
–
Child 1.Create a file named myuuid.vbs with the content
set obj = CreateObject("Scriptlet.TypeLib")
WScript.StdOut.WriteLine Mid(obj.GUID, 2, 36)
2.goto command prompt
cscript //NoLogo myuuid.vbs
Using JAVA code
UUID uuid = UUID.randomUUID();
String randomUUIDString = uuid.toString();
uuidgen
command. –
Budbudapest This will copy a new GUID to your clipboard:
POWERSHELL -c "[guid]::NewGuid().ToString().ToUpper()" | CLIP
Recent versions have cmdlets like this:
POWERSHELL -c "New-Guid | Set-Clipboard"
There is no built-in command available that does that. Either write your own, or get an existing one.
A simple program that outputs a GUID to the console could be written using C#:
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine(System.Guid.NewGuid().ToString());
}
}
Place the above snippet in a file name guidgen.cs and then compile it using the following command line (.NET Framework 2.0 would have to be installed on your system):
%WINDIR%\Microsoft.NET\Framework\v2.0.50727\csc.exe guidgen.cs
This will create an executable named guidgen.exe
.
clip
the output in bash as $ guidgen | clip
? I'm having to wrap this in a function and export it as guidgen() { echo "$(guidgen.exe)"; } export -f guidgen
(see #1500999). –
Substratosphere If you want to do it with pure cmd commands, you can use something like that (this is not a true GUID but it can help depending on your context) :
@call :GetGuid NewGuid
@echo My new GUID : %NewGuid%
@goto :eof
:GetGuid
@set _guid=%computername%%date%%time%
@set _guid=%_guid:/=%
@set _guid=%_guid:.=%
@set _guid=%_guid: =%
@set _guid=%_guid:,=%
@set _guid=%_guid::=%
@set _guid=%_guid:-=%
@set %1=%_guid%
@goto :eof
@echo off
at the beginning instead of prefixing every commands with @
? –
Impoverished try with uuid.bat
Without arguments it will just echo the generated uuid
:
c:\>uuid.bat
2186cb38-d649-4c99-b7cc-c505e4dae9c2
If argument is passed it will be stored in a variable with the same name:
call uuid.bat my_uuid
echo %my_uuid%
94772f66-8bca-468e-9e9a-de0d9ee05cc1
For windows formatted GUIDs you can use guid.bat:
for /f %a in ('guid.bat') do set guid=%a
echo %guid%
If you have dotnet
installed:
$ dotnet tool install -g guid
$ guid
bf4be9d0-7d1b-485c-a435-d07fd7b892f0
$ guid help
Usage:
guid [option]
Where [option] is an optional single character that controls formatting:
N B382C3F44E604D08B48A2D342A659B4E
D B382C3F4-4E60-4D08-B48A-2D342A659B4E
B {B382C3F4-4E60-4D08-B48A-2D342A659B4E}
P (B382C3F4-4E60-4D08-B48A-2D342A659B4E)
When unspecified, 'd' formatting is used. Use lowercase [option] for lowercase output.
One could abuse bitsadmin
to generate a GUID. This will be profoundly faster than calling PowerShell from a Batch script.
@echo off & setlocal
for /f "delims={}" %%I in ('bitsadmin /rawreturn /create guid') do set "GUID=%%~I"
>NUL bitsadmin /cancel {%GUID%}
echo Your new GUID is %GUID%. Neat.
If you want to keep the surrounding braces, remove "delims={}"
and replace {%GUID%}
with %GUID%
.
If the system OS does not have Windows SDK but does have a C compiler with mingw-w64 toolchain then compile this small program to generate random GUID. Imported functions are UuidCreate (rpcrt4.lib) to create random UUID and StringFromCLSID (ole32.lib) to convert UUID to wide string.
#include <Windows.h>
#include <stdio.h>
/*
* int UuidCreate(GUID *id);
* int StringFromCLSID(GUID *id, wchar_t **str);
* Libraries: Rpcrt4.lib Ole32.lib
*/
int main(void)
{
GUID id;
wchar_t *str = NULL;
UuidCreate(&id);
StringFromCLSID(&id, &str);
wprintf(L"%ls\n", str);
}
© 2022 - 2024 — McMap. All rights reserved.