Generate GUID with New-Guid Cmdlet
PowerShell 5.0+ includes the New-Guid cmdlet for creating GUIDs (Microsoft's term for UUIDs). This native command generates random GUIDs without requiring any .NET assembly loading, making it the simplest and recommended approach for modern PowerShell scripts.
# Method 1: New-Guid cmdlet (PowerShell 5.0+)
$guid = New-Guid
Write-Host $guid
# Output: d40e8400-e29b-41d4-a716-446655440000
# Get as string
$guidString = $guid.ToString()
# Generate and use immediately
$sessionId = (New-Guid).ToString()
# Uppercase format
$upperGuid = (New-Guid).ToString().ToUpper()
PowerShell Version Note
New-Guid is available in PowerShell 5.0 and later. For older versions (3.0-4.0), use the .NET method: [guid]::NewGuid()
Alternative: .NET GUID Generation Method
For PowerShell versions prior to 5.0 or when you need direct .NET integration, use the [guid]::NewGuid() .NET class method. This approach works across all PowerShell versions and provides identical functionality to New-Guid.
# Method 2: .NET class (all PowerShell versions)
$guid = [guid]::NewGuid()
$guid
# Store in variable
$newGuid = [guid]::NewGuid()
# Get as string
$guidText = [guid]::NewGuid().ToString()
# Both methods produce identical results
$method1 = New-Guid
$method2 = [guid]::NewGuid()
# Both generate random GUIDs (UUID v4)
GUID String Formatting Options
PowerShell GUIDs support multiple string formats including default (with hyphens), without hyphens, with braces, and with parentheses. Choose the format that matches your application's requirements - databases often prefer the default format, while some APIs require specific formatting.
Standard Formats
$guid = New-Guid
# Default with hyphens
$guid.ToString()
# d40e8400-e29b-41d4-a716-446655440000
# No hyphens
$guid.ToString("N")
# d40e8400e29b41d4a716446655440000
# With braces
$guid.ToString("B")
# {d40e8400-e29b-41d4-a716-446655440000}
Special Formats
# With parentheses
$guid.ToString("P")
# (d40e8400-e29b-41d4-a716-446655440000)
# Uppercase
$guid.ToString().ToUpper()
# D40E8400-E29B-41D4-A716-446655440000
# Lowercase
$guid.ToString().ToLower()
# d40e8400-e29b-41d4-a716-446655440000
Generate Multiple GUIDs with PowerShell Pipeline
PowerShell's pipeline makes it easy to generate multiple GUIDs at once. This is particularly useful for batch operations, test data generation, or when you need to create unique identifiers for multiple resources simultaneously.
# Generate 10 GUIDs
1..10 | ForEach-Object { New-Guid }
# Store in array
$guids = 1..10 | ForEach-Object { New-Guid }
# Generate and display with index
1..5 | ForEach-Object {
[PSCustomObject]@{
Index = $_
GUID = (New-Guid).ToString()
}
}
# Export to file
1..100 | ForEach-Object { New-Guid } | Out-File -FilePath "guids.txt"
# Export to CSV
1..50 | ForEach-Object {
[PSCustomObject]@{
ID = $_
GUID = (New-Guid).ToString()
Timestamp = Get-Date
}
} | Export-Csv -Path "guids.csv" -NoTypeInformation
Parse and Validate GUID Strings
Validating GUID strings from user input or external sources is essential for data integrity in PowerShell scripts. Use try-catch blocks with [guid]::Parse() to safely validate and convert GUID strings.
# Parse from string
$guidString = "550e8400-e29b-41d4-a716-446655440000"
$guid = [guid]$guidString
# Validation function
function Test-Guid {
param([string]$GuidString)
try {
[guid]::Parse($GuidString) | Out-Null
return $true
}
catch {
return $false
}
}
# Test validation
Test-Guid "550e8400-e29b-41d4-a716-446655440000" # True
Test-Guid "invalid-guid" # False
Test-Guid "12345" # False
# Empty/Nil GUID
$emptyGuid = [guid]::Empty
Write-Host $emptyGuid # 00000000-0000-0000-0000-000000000000
# Check if GUID is empty
if ($guid -eq [guid]::Empty) {
Write-Host "GUID is empty"
}
Active Directory and Windows Automation Examples
GUIDs are extensively used in Windows administration for Active Directory objects, computer names, and resource identifiers. PowerShell's GUID generation integrates seamlessly with Active Directory cmdlets and Windows management tasks.
Generate Unique Computer Names
# Create unique computer name with GUID
$guid = (New-Guid).ToString().Split('-')[0]
$computerName = "WS-$guid"
Write-Host "New computer name: $computerName"
# Output: WS-d40e8400
Create Unique File Names
# Generate unique log file name
$guid = New-Guid
$logFile = "log-$guid.txt"
New-Item -Path $logFile -ItemType File
# Timestamped with GUID
$timestamp = Get-Date -Format "yyyyMMdd"
$fileName = "backup-$timestamp-$((New-Guid).ToString('N').Substring(0,8)).zip"
Session ID Tracking
# Create session with unique ID
$session = [PSCustomObject]@{
SessionID = (New-Guid).ToString()
User = $env:USERNAME
StartTime = Get-Date
ComputerName = $env:COMPUTERNAME
}
Write-Host "Session created: $($session.SessionID)"