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

如何通过PowerShell命令增加TXT文件中列的间距?

解决PowerShell导出TXT列间距过近的问题

方案1:固定列宽强制分配间距

直接通过Format-Table为每列指定固定宽度,确保所有13列都能显示且间距充足,避免数据截断或表头异常。示例代码:

# 定义需要导出的13列AD用户属性(可根据实际需求调整)
$targetProps = @(
    'SamAccountName', 'Enabled', 'whenChanged', 'DistinguishedName',
    'Name', 'GivenName', 'Surname', 'UserPrincipalName',
    'AccountExpirationDate', 'LastLogonDate', 'Department', 'Title', 'EmailAddress'
)

# 获取用户数据并筛选目标属性
$output = Get-ADUser -Filter "SamAccountName -eq 'jared.h'" -Properties $targetProps | Select $targetProps

# 为每列设置固定宽度,预留足够间距,确保所有列完整显示
$output | Format-Table @(
    @{Label='SamAccountName'; Expression={$_.SamAccountName}; Width=20},
    @{Label='Enabled'; Expression={$_.Enabled}; Width=10},
    @{Label='whenChanged'; Expression={$_.whenChanged}; Width=25},
    @{Label='DistinguishedName'; Expression={$_.DistinguishedName}; Width=80},
    @{Label='Name'; Expression={$_.Name}; Width=30},
    @{Label='GivenName'; Expression={$_.GivenName}; Width=20},
    @{Label='Surname'; Expression={$_.Surname}; Width=20},
    @{Label='UserPrincipalName'; Expression={$_.UserPrincipalName}; Width=40},
    @{Label='AccountExpirationDate'; Expression={$_.AccountExpirationDate}; Width=25},
    @{Label='LastLogonDate'; Expression={$_.LastLogonDate}; Width=25},
    @{Label='Department'; Expression={$_.Department}; Width=30},
    @{Label='Title'; Expression={$_.Title}; Width=40},
    @{Label='EmailAddress'; Expression={$_.EmailAddress}; Width=40}
) | Out-File -FilePath C:\output.txt -Width 5000
  • 注意:Width参数直接指定列的显示宽度,所有列宽度总和需小于Out-File设置的-Width值(这里设为5000完全足够)。如果之前设置Width后数据丢失,可能是宽度设置过小,可根据实际内容调整对应列的宽度值。

方案2:用字符串拼接手动控制间距

如果Format-Table的宽度设置仍有异常,可以手动拼接每一行的内容,完全自定义列间距:

$targetProps = @(
    'SamAccountName', 'Enabled', 'whenChanged', 'DistinguishedName',
    'Name', 'GivenName', 'Surname', 'UserPrincipalName',
    'AccountExpirationDate', 'LastLogonDate', 'Department', 'Title', 'EmailAddress'
)

$output = Get-ADUser -Filter "SamAccountName -eq 'jared.h'" -Properties $targetProps | Select $targetProps

# 生成表头,每个表头字段填充到指定宽度,用两个空格分隔
$header = @(
    'SamAccountName'.PadRight(20),
    'Enabled'.PadRight(10),
    'whenChanged'.PadRight(25),
    'DistinguishedName'.PadRight(80),
    'Name'.PadRight(30),
    'GivenName'.PadRight(20),
    'Surname'.PadRight(20),
    'UserPrincipalName'.PadRight(40),
    'AccountExpirationDate'.PadRight(25),
    'LastLogonDate'.PadRight(25),
    'Department'.PadRight(30),
    'Title'.PadRight(40),
    'EmailAddress'.PadRight(40)
) -join '  '

# 生成数据行,每个字段填充到对应宽度
$dataLines = $output | ForEach-Object {
    $fields = @(
        $_.SamAccountName.PadRight(20),
        $_.Enabled.ToString().PadRight(10),
        $_.whenChanged.ToString().PadRight(25),
        $_.DistinguishedName.PadRight(80),
        $_.Name.PadRight(30),
        $_.GivenName.PadRight(20),
        $_.Surname.PadRight(20),
        $_.UserPrincipalName.PadRight(40),
        $_.AccountExpirationDate.ToString().PadRight(25),
        $_.LastLogonDate.ToString().PadRight(25),
        $_.Department.PadRight(30),
        $_.Title.PadRight(40),
        $_.EmailAddress.PadRight(40)
    ) -join '  '
    $fields
}

# 将表头和数据行合并写入文件
@($header) + $dataLines | Out-File -FilePath C:\output.txt -Encoding UTF8
  • 这种方式完全由你控制每列的宽度和间距,不会出现表头错位或数据丢失的问题,可读性极强。

方案3:导出为CSV后转换为带间距的文本(兼容复杂数据)

如果存在特殊字符或超长字段,可先导出为CSV再转换,避免格式混乱:

$targetProps = @(
    'SamAccountName', 'Enabled', 'whenChanged', 'DistinguishedName',
    'Name', 'GivenName', 'Surname', 'UserPrincipalName',
    'AccountExpirationDate', 'LastLogonDate', 'Department', 'Title', 'EmailAddress'
)

$output = Get-ADUser -Filter "SamAccountName -eq 'jared.h'" -Properties $targetProps | Select $targetProps

# 先导出为CSV临时文件
$output | ConvertTo-Csv -NoTypeInformation | Out-File C:\temp_user.csv -Encoding UTF8

# 读取CSV内容并转换为带间距的文本
$csvContent = Get-Content C:\temp_user.csv
$headerFields = $csvContent[0].Split(',') | ForEach-Object { $_.Trim('"').PadRight(20) }
$headerLine = $headerFields -join '  '

$dataLines = $csvContent[1..($csvContent.Count-1)] | ForEach-Object {
    $fields = $_.Split(',') | ForEach-Object { $_.Trim('"').PadRight(20) }
    $fields -join '  '
}

# 写入最终文件
@($headerLine) + $dataLines | Out-File C:\output.txt -Encoding UTF8

# 删除临时CSV文件(可选)
Remove-Item C:\temp_user.csv
  • 此方法适合处理包含逗号、引号等特殊字符的数据,确保转换过程中数据不丢失。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 10:35:39