diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewGuidCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewGuidCommand.cs
index 537d86c24e..3aa070bcd6 100644
--- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewGuidCommand.cs
+++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewGuidCommand.cs
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
+#nullable enable
+
using System;
using System.Management.Automation;
@@ -9,16 +11,48 @@ namespace Microsoft.PowerShell.Commands
///
/// The implementation of the "new-guid" cmdlet.
///
- [Cmdlet(VerbsCommon.New, "Guid", HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2097130")]
+ [Cmdlet(VerbsCommon.New, "Guid", DefaultParameterSetName = "Default", HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2097130")]
[OutputType(typeof(Guid))]
- public class NewGuidCommand : Cmdlet
+ public class NewGuidCommand : PSCmdlet
{
///
- /// Returns a guid.
+ /// Gets or sets a value indicating that the cmdlet should return a Guid structure whose value is all zeros.
///
- protected override void EndProcessing()
+ [Parameter(ParameterSetName = "Empty")]
+ public SwitchParameter Empty { get; set; }
+
+ ///
+ /// Gets or sets the value to be converted to a Guid.
+ ///
+ [Parameter(Position = 0, ValueFromPipeline = true, ParameterSetName = "InputObject")]
+ [System.Diagnostics.CodeAnalysis.AllowNull]
+ public string InputObject { get; set; }
+
+ ///
+ /// Returns a Guid.
+ ///
+ protected override void ProcessRecord()
{
- WriteObject(Guid.NewGuid());
+ Guid? guid = null;
+
+ if (ParameterSetName is "InputObject")
+ {
+ try
+ {
+ guid = new(InputObject);
+ }
+ catch (Exception ex)
+ {
+ ErrorRecord error = new(ex, "StringNotRecognizedAsGuid", ErrorCategory.InvalidArgument, null);
+ WriteError(error);
+ }
+ }
+ else
+ {
+ guid = ParameterSetName is "Empty" ? Guid.Empty : Guid.NewGuid();
+ }
+
+ 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 e1d0431df4..54d609d275 100644
--- a/test/powershell/Modules/Microsoft.PowerShell.Utility/New-Guid.Tests.ps1
+++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/New-Guid.Tests.ps1
@@ -2,17 +2,43 @@
# Licensed under the MIT License.
Describe "New-Guid" -Tags "CI" {
- It "returns a new guid" {
+ It "Returns a new guid" {
$guid = New-Guid
$guid | Should -BeOfType System.Guid
}
- It "should not be all zeros" {
+ It "Should not be all zeros" {
$guid = New-Guid
$guid.ToString() | Should -Not -BeExactly "00000000-0000-0000-0000-000000000000"
}
- It "should return different guids with each call" {
+ It "Should be all zeros" {
+ $guid = New-Guid -Empty
+ $guid.ToString() | Should -BeExactly "00000000-0000-0000-0000-000000000000"
+ }
+
+ It "Should convert a string to a guid" {
+ $guid1 = New-Guid
+ $guid2 = New-Guid -InputObject $guid1.ToString()
+ $guid3 = New-Guid $guid1.ToString()
+ $guid2 | Should -BeOfType System.Guid
+ $guid1.ToString() | Should -BeExactly $guid2.ToString()
+ $guid1.ToString() | Should -BeExactly $guid3.ToString()
+ }
+
+ It "Should convert a string to a guid, value from pipeline" {
+ $guids = '11c43ee8-b9d3-4e51-b73f-bd9dda66e29c','0f8fad5bd9cb469fa16570867728950e' | New-Guid
+ $guids[0].ToString() | Should -BeExactly '11c43ee8-b9d3-4e51-b73f-bd9dda66e29c'
+ $guids[1].ToString() | Should -BeExactly '0f8fad5b-d9cb-469f-a165-70867728950e'
+ }
+
+ It "Should accept pipeline input" {
+ $guids = 1..10 | foreach-object { [guid]::newguid() }
+ $observed = $guids.Foreach({$_.ToString()}) | New-Guid
+ $observed | Should -Be $guids
+ }
+
+ It "Should return different guids with each call" {
$guid1 = New-Guid
$guid2 = New-Guid
$guid1.ToString() | Should -Not -Be $guid2.ToString()