From bb0794bd0ecb36e58f1ee0f103ad01f43af86333 Mon Sep 17 00:00:00 2001 From: SufficientDaikon Date: Sun, 15 Mar 2026 22:47:28 +0200 Subject: [PATCH] Change New-Guid to generate UUID v7 by default Replace `Guid.NewGuid()` with `Guid.CreateVersion7()` in the New-Guid cmdlet. UUID v7 (RFC 9562) embeds a millisecond-precision timestamp making generated GUIDs monotonically sortable, which is ideal for database primary keys and distributed systems. Users who specifically need UUID v4 can call [Guid]::NewGuid() directly. Fixes #24895 Co-Authored-By: Claude Opus 4.6 --- .../commands/utility/NewGuidCommand.cs | 2 +- .../Modules/Microsoft.PowerShell.Utility/New-Guid.Tests.ps1 | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewGuidCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewGuidCommand.cs index 0e466f86d3f..d1d41b312f7 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewGuidCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewGuidCommand.cs @@ -49,7 +49,7 @@ protected override void ProcessRecord() } else { - guid = Empty.ToBool() ? Guid.Empty : Guid.NewGuid(); + guid = Empty.ToBool() ? Guid.Empty : Guid.CreateVersion7(); } WriteObject(guid); diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/New-Guid.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/New-Guid.Tests.ps1 index 8756fed7726..9b082793413 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/New-Guid.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/New-Guid.Tests.ps1 @@ -43,6 +43,12 @@ Describe "New-Guid" -Tags "CI" { $observed | Should -Be $guids } + It "Should generate a UUID v7" { + $guid = New-Guid + # UUID v7 has version nibble '7' at position 14 in the string representation + $guid.ToString()[14] | Should -BeExactly '7' + } + It "Should return different guids with each call" { $guid1 = New-Guid $guid2 = New-Guid