How to Generate a UUID in PowerShell
The fastest way to generate a UUID in PowerShell is [guid]::NewGuid(), which calls .NET's System.Guid generator directly and works in every version back to Windows PowerShell 5.1. PowerShell 7+ (and recent Windows PowerShell builds) also ship the friendlier New-Guid cmdlet, which returns the same kind of object with more readable syntax. See the rust uuid page for Cargo.toml setup, type-safe UUID generation, and Diesel ORM integration.
$myGuid = [guid]::NewGuid()
$myGuid
# Output: 550e8400-e29b-41d4-a716-446655440000
$myGuid2 = New-Guid
$myGuid2
# Output: 6ba7b810-9dad-11d1-80b4-00c04fd430c8
Explanation
[guid]::NewGuid()calls .NET'sSystem.Guid.NewGuid()static method directly, so it runs unmodified on Windows PowerShell 5.1 and every PowerShell 7+ release.New-Guidis a cmdlet, introduced in PowerShell 5.0, that wraps the same .NET method with cmdlet-style syntax and pipeline support.- Both return a
[guid]object, not a plain string — use.ToString()or string interpolation (like"$myGuid") when you need the 36-character text form. - Each call produces a random UUID version 4, with no external module or
Install-Modulerequired.
Convert a String to a UUID in PowerShell
Sometimes a UUID arrives as plain text — from a config file, an API response, or user input — and you need it back as a proper [guid] object. PowerShell gives you three ways to do this, each with a different tradeoff for handling invalid input. For SwiftUI @Observable models with UUID primary keys, the swift uuid guide has complete implementation examples.
$uuidString = "550e8400-e29b-41d4-a716-446655440000"
# Simple cast - throws if the string is invalid
$guid = [guid]$uuidString
# [guid]::Parse() - same behavior, explicit method call
$guid = [guid]::Parse($uuidString)
# [guid]::TryParse() - safe for untrusted input, returns $true/$false
$result = [guid]::Empty
if ([guid]::TryParse($uuidString, [ref]$result)) {
Write-Host "Valid UUID: $result"
} else {
Write-Host "Invalid UUID string"
}
Explanation
[guid]$uuidStringcasts the string directly to a[guid]object — the shortest syntax, but it throws an exception if the string isn't a valid UUID.[guid]::Parse($uuidString)behaves identically to the cast but reads more explicitly as a parse operation in longer scripts.[guid]::TryParse()never throws — it returns a boolean and writes the parsed value into the[ref]parameter, making it the safer choice when validating input you don't control.- All three methods accept UUIDs with or without surrounding braces or hyphens, matching every format .NET's
System.Guidparser understands.
Other Options for Generating UUIDs in PowerShell
PowerShell's [guid] type is backed by .NET's System.Guid, which only generates version 4 (random) UUIDs natively — until .NET 9. If you're running PowerShell 7.4+ on a .NET 9 runtime, a newer, time-sortable UUID version 7 option is available too.
# Requires PowerShell 7.4+ running on .NET 9
$uuidV7 = [guid]::CreateVersion7()
$uuidV7
# Output: 018f4f3a-7b2c-7c11-9a4d-1e2f3a4b5c6d
Explanation
[guid]::CreateVersion7()was added in .NET 9 and encodes the current Unix millisecond timestamp in the leading bits, so IDs generated later always sort after earlier ones.- Version 7 UUIDs suit database primary keys and indexes well, since sequential inserts avoid the random-order page splits that version 4 UUIDs can cause.
- This method isn't available on older .NET runtimes — check
$PSVersionTable.PSVersionand the underlying CLR version before relying on it in production scripts. - For everything else — session tokens, file names, general-purpose unique IDs —
[guid]::NewGuid()orNew-Guidremains the simpler default.