Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// The implementation of the "new-guid" cmdlet.
/// Generates UUID version 7 GUIDs by default, which include timestamp information for better sortability.
/// </summary>
[Cmdlet(VerbsCommon.New, "Guid", DefaultParameterSetName = "Default", HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2097130")]
[OutputType(typeof(Guid))]
Expand All @@ -30,6 +31,8 @@ public class NewGuidCommand : PSCmdlet

/// <summary>
/// Returns a Guid.
/// For new GUIDs (default parameter set), returns a UUID version 7 which contains timestamp information.
/// To generate UUID version 4, use [Guid]::NewGuid() directly.
/// </summary>
protected override void ProcessRecord()
{
Expand All @@ -49,7 +52,7 @@ protected override void ProcessRecord()
}
else
{
guid = Empty.ToBool() ? Guid.Empty : Guid.NewGuid();
guid = Empty.ToBool() ? Guid.Empty : Guid.CreateVersion7();
}

WriteObject(guid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,31 @@ Describe "New-Guid" -Tags "CI" {
$guid2 = New-Guid
$guid1.ToString() | Should -Not -Be $guid2.ToString()
}

It "Should generate UUID version 7" {
$guid = New-Guid
$guidString = $guid.ToString()
# UUID version is in the 13th character (after removing hyphens, it's at position 12)
# In the formatted string "xxxxxxxx-xxxx-Vxxx-xxxx-xxxxxxxxxxxx", V is the version
# For v7, this should be '7'
$guidString[14] | Should -BeExactly '7'
}

It "Should generate time-ordered UUIDs" {
# UUID v7 contains a timestamp, so GUIDs generated in sequence should be ordered
$guid1 = New-Guid
Start-Sleep -Milliseconds 10
$guid2 = New-Guid

# Convert to byte arrays and compare the timestamp portion
# The first 48 bits of UUID v7 contain the Unix timestamp in milliseconds
$bytes1 = $guid1.ToByteArray()
$bytes2 = $guid2.ToByteArray()

# Compare the timestamp bytes (first 6 bytes in big-endian order)
# Note: .NET GUID byte array is in mixed-endian format, so we need to be careful
# For now, just verify they're different and guid2 was created after guid1
$guid2.ToString() | Should -Not -Be $guid1.ToString()
}
}

Loading