Edit

New-Guid

Creates a GUID.

Syntax

Default (Default)

New-Guid
    [<CommonParameters>]

Empty

New-Guid
    [-Empty]
    [<CommonParameters>]

InputObject

New-Guid
    [-InputObject <String>]
    [<CommonParameters>]

Description

The New-Guid cmdlet creates a Version 7 globally unique identifier (GUID). Version 7 UUIDs contain a millisecond-precision timestamp and are sortable. If you need a unique ID in a script, you can create a GUID, as needed.

Note

In PowerShell 7.5 and earlier, New-Guid created Version 4 (random) UUIDs. Starting in PowerShell 7.6, the default changed to Version 7. If you need a Version 4 UUID, use [guid]::NewGuid() directly.

Examples

Example 1: Create a new GUID

New-Guid

This command creates a GUID. Alternatively, you could store the output of this cmdlet in a variable to use elsewhere in a script.

Example 2: Create an empty GUID

New-Guid -Empty
Guid
----
00000000-0000-0000-0000-000000000000

Example 3: Create a GUID from a string

This example converts a string that contains a GUID to a GUID object.

New-Guid -InputObject "d61bbeca-0186-48fa-90e1-ff7aa5d33e2d"
Guid
----
d61bbeca-0186-48fa-90e1-ff7aa5d33e2d

Example 4: Convert strings from the pipeline to GUIDs

This examples converts strings from the pipeline to GUID objects.

$guidStrings = (
'11c43ee8-b9d3-4e51-b73f-bd9dda66e29c',
'0f8fad5bd9cb469fa16570867728950e',
'{0x01234567, 0x89ab, 0xcdef,{0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef}}'
)
$guidStrings | New-Guid
Guid
----
11c43ee8-b9d3-4e51-b73f-bd9dda66e29c
0f8fad5b-d9cb-469f-a165-70867728950e
01234567-89ab-cdef-0123-456789abcdef

Example 5: Create specific UUID versions using .NET APIs

This example shows how to create specific UUID versions using .NET APIs directly.

[guid]::CreateVersion7()
[guid]::NewGuid()
Guid
----
019588a4-dbe2-7f30-8b9f-4a1c0e5d3a28
d61bbeca-0186-48fa-90e1-ff7aa5d33e2d

The version number appears in the third group of the GUID string. Version 7 UUIDs start with a 7 in that position (7f30), while Version 4 UUIDs show a 4 (48fa).

Parameters

-Empty

Indicates that this cmdlet creates an empty GUID. An empty GUID has all zeros in its string.

Parameter properties

Type:SwitchParameter
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

Empty
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-InputObject

This parameter accepts a string representing a GUID and converts it to a GUID object.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

InputObject
Position:Named
Mandatory:False
Value from pipeline:True
Value from pipeline by property name:False
Value from remaining arguments:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

Outputs

Guid

This cmdlet returns a GUID.

Notes

The cmdlet passes string input to the constructor of the System.Guid class. The constructor support strings in several formats. For more information, see System.Guid(String).

When used without string input or the Empty parameter, the cmdlet creates a Version 7 Universally Unique Identifier (UUID) as defined in RFC 9562.

In PowerShell 7.5 and earlier, the cmdlet created a Version 4 (random) UUID. If you need a Version 4 UUID, use [guid]::NewGuid(). To explicitly create a Version 7 UUID, use [guid]::CreateVersion7().

For more information, see System.Guid.CreateVersion7.