How to Generate a GUID in PowerShell
By SimpleTools24Published 2 min read
To generate a GUID in PowerShell, run New-Guid, or [guid]::NewGuid() on any version. Both return a System.Guid object; add .Guid or .ToString() to get the text. For example, (New-Guid).Guid prints a new random GUID such as 3f2b8c1e-9d4a-4f6e-8b1a-2c7d5e9f0a13. Format specifiers like "N" and "B" give compact or braced versions.
In this guide
Two Ways to Create a GUID
New-Guid
# Guid
# ----
# 3f2b8c1e-9d4a-4f6e-8b1a-2c7d5e9f0a13
[guid]::NewGuid()New-Guid is a cmdlet available in Windows PowerShell 5.0 and later and in PowerShell 7 on Windows, macOS and Linux. [guid]::NewGuid() calls the .NET method directly and works everywhere PowerShell runs. Both produce random version 4 GUIDs.
Getting Just the String
The default output is a table with a "Guid" header. To get plain text:
$id = (New-Guid).Guid
$id = [guid]::NewGuid().ToString()
"$(New-Guid)" # string interpolation also worksFormatting Options
$g = New-Guid
$g.ToString("N") # 3f2b8c1e9d4a4f6e8b1a2c7d5e9f0a13
$g.ToString("B") # {3f2b8c1e-9d4a-4f6e-8b1a-2c7d5e9f0a13}
$g.ToString("P") # (3f2b8c1e-9d4a-4f6e-8b1a-2c7d5e9f0a13)
$g.ToString().ToUpper() # 3F2B8C1E-9D4A-4F6E-8B1A-2C7D5E9F0A13| Need | Expression |
|---|---|
| Standard lowercase | (New-Guid).Guid |
| No hyphens | (New-Guid).ToString("N") |
| Braces (registry, MSI, config) | (New-Guid).ToString("B") |
| Uppercase braces | (New-Guid).ToString("B").ToUpper() |
Copy a GUID to the Clipboard
(New-Guid).Guid | Set-ClipboardSet-Clipboard is available on Windows PowerShell 5.0+ and PowerShell 7 on Windows. On macOS and Linux, pipe to pbcopy or xclip instead.
Generating Many GUIDs
1..100 | ForEach-Object { (New-Guid).Guid }
# Save to a file
1..1000 | ForEach-Object { (New-Guid).Guid } | Set-Content guids.txt
# As CSV with a header
1..50 | ForEach-Object { [pscustomobject]@{ Id = (New-Guid).Guid } } |
Export-Csv ids.csv -NoTypeInformationValidating a GUID String
$text = "{3F2B8C1E-9D4A-4F6E-8B1A-2C7D5E9F0A13}"
$parsed = [guid]::Empty
if ([guid]::TryParse($text, [ref]$parsed)) {
"Valid: $parsed"
} else {
"Not a GUID"
}TryParse accepts several formats, including braces and the compact N form. For a stricter check, such as requiring the standard hyphenated form and a valid version digit, use a regular expression; UUID regex patterns has tested examples.
Time-Ordered GUIDs
PowerShell 7 runs on modern .NET, so on PowerShell versions built on .NET 9 or later you can call [guid]::CreateVersion7() for time-ordered GUIDs. Check your runtime with $PSVersionTable. Windows PowerShell 5.1 runs on .NET Framework and doesn't have this method. How to generate a GUID in C# covers version 7 in .NET.
Practical Uses in Scripts
- Unique file or folder names for temporary output
- Correlation IDs in log entries across script steps
- Product and component codes in installer projects, which commonly use braced uppercase GUIDs
- Test data for databases and APIs
Without a Shell
If you're on a machine without PowerShell, or you need a quick batch for a document or spreadsheet, the GUID generator creates up to 1,000 GUIDs in your browser, in standard, uppercase, braced or compact style, and downloads them as TXT, CSV or JSON. For GUIDs inside spreadsheets specifically, see how to generate GUIDs in Excel and Google Sheets.
Real-World Script Examples
Unique Temporary Folders
$work = Join-Path $env:TEMP ("build-" + (New-Guid).ToString("N"))
New-Item -ItemType Directory -Path $work | Out-Null
# ... do work ...
Remove-Item $work -Recurse -ForceA GUID in the folder name means parallel runs of the same script never collide.
Correlation IDs in Logs
$runId = (New-Guid).Guid
Write-Output "[$runId] Starting import"
# pass $runId to every log line and child scriptWhen several scripts run at once, filtering logs by the run ID shows one run's story from start to finish.
Seeding Test Records
$users = 1..20 | ForEach-Object {
[pscustomobject]@{ Id = (New-Guid).Guid; Name = "Test User $_" }
}
$users | ConvertTo-Json | Set-Content users.jsonTroubleshooting
- "New-Guid is not recognized": you're on Windows PowerShell older than 5.0. Use
[guid]::NewGuid()instead. - Output shows a "Guid" header: you're printing the object. Use
.Guidor.ToString(). - Values look identical in a loop: check that you're calling
New-Guidinside the loop, not assigning one value before it. - Braces missing in a config file: use the "B" format and match the case the file expects.
Frequently Asked Questions
What version of GUID does New-Guid create?
A random version 4 GUID.
How do I generate a GUID without hyphens in PowerShell?
(New-Guid).ToString("N").
Does New-Guid work on Linux and macOS?
Yes, in PowerShell 7.
Is a PowerShell GUID secure enough for passwords?
No. Use it for unique identifiers, not secrets.
Summary
New-Guid or [guid]::NewGuid() creates a GUID; .Guid or .ToString(format) turns it into text. Loop for bulk generation, TryParse for validation, and Set-Clipboard for quick copying.
Related tools
Sources
- New-Guid – Microsoft Learn (opens in a new tab)
Related articles
- Generate a GUID in C#Guid.NewGuid(), Guid.CreateVersion7(), format specifiers, parsing and the mistakes to avoid in .NET.
- GUIDs in Excel and Google SheetsFormula, Power Query, VBA and Apps Script methods for GUIDs in spreadsheets, and why values keep changing.
- GUID vs UUIDSame 128-bit identifier, two names. Where each term comes from, .NET formatting and byte order, and which version to use.