You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell导出含GUID的SQL数据集至CSV时,如何将唯一标识符转为大写

How to Export SQL Dataset with Uppercase GUIDs in PowerShell?

When exporting a SQL dataset where the first column contains unique identifiers (GUIDs), PowerShell/.NET automatically converts GUIDs to lowercase. Since the downstream system requires GUIDs to be in uppercase, what's the cleanest way to achieve this? The current export code is:
$DataSet.Tables[0] | Export-Csv $OuputFile -NoTypeInformation

Cleanest Ways to Fix This

1. Transform the GUID Column Directly (Most Straightforward)

The simplest and most readable approach is to tweak the GUID values as part of your pipeline before exporting. Use Select-Object to target either the known column name or the first column dynamically, converting each GUID to uppercase:

# If you know the exact column name (swap 'GuidColumn' with your actual column name)
$DataSet.Tables[0] | 
  Select-Object @{Name='GuidColumn'; Expression={$_.GuidColumn.ToString().ToUpper()}}, * -ExcludeProperty GuidColumn |
  Export-Csv $OutputFile -NoTypeInformation

# If you need to target the first column dynamically (no hardcoded name)
$firstCol = $DataSet.Tables[0].Columns[0].ColumnName
$DataSet.Tables[0] | 
  Select-Object @{Name=$firstCol; Expression={$_.$firstCol.ToString().ToUpper()}}, * -ExcludeProperty $firstCol |
  Export-Csv $OutputFile -NoTypeInformation

2. Reusable Type Converter (For Multiple Scripts/Datasets)

If you're dealing with this requirement across multiple datasets or scripts, a custom type converter will make this behavior automatic without repeating code each time:

# Add a custom type converter for GUIDs that outputs uppercase
Add-Type @"
using System;
using System.ComponentModel;
public class UppercaseGuidConverter : TypeConverter {
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
        if (value is Guid guid && destinationType == typeof(string)) {
            return guid.ToString("D").ToUpper();
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}
"@

# Attach the converter to your first (GUID) column
$DataSet.Tables[0].Columns[0].ExtendedProperties["TypeConverter"] = [UppercaseGuidConverter]::new()

# Now export like normal—GUIDs will be uppercase automatically
$DataSet.Tables[0] | Export-Csv $OutputFile -NoTypeInformation

3. Post-Process the CSV (Quick One-Liner Hack)

If you don't want to modify the dataset itself, export normally then do a find-and-replace on the CSV file. Just note this works best if your CSV doesn't have other lowercase strings that match GUID formatting:

# Export as usual first
$DataSet.Tables[0] | Export-Csv $OutputFile -NoTypeInformation

# Replace all lowercase GUIDs with uppercase versions
(Get-Content $OutputFile) -replace '\b[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}\b', { $_.Value.ToUpper() } | Set-Content $OutputFile

My Recommendation

Stick with the first method for one-off tasks—it's efficient, easy to read, and avoids extra steps like file I/O or custom type setup. If you need this behavior consistently across multiple projects, the type converter approach will save you time in the long run.

内容的提问来源于stack exchange,提问作者Alec Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 09:32:44