【问题标题】:PowerShell is re-arranging my columns and i cant figure out whyPowerShell 正在重新排列我的列,我不知道为什么
【发布时间】:2022-01-23 18:58:19
【问题描述】:

我使用的代码如下

    New-Object PSObject -Property @{
        Product = $_.Name 
        Stockcode = $_.Group[0].Stockcode
        QuantityCounted = ($_.Group | Measure-Object -Property QuantityCounted -Sum).Sum    
    }
}| Export-Csv "E:\CSV\test.csv" -NoTypeInformation  

标题应按此顺序 产品 |股票代码 |数量统计

如果有人能指出我正确的方向,那就太棒了,谢谢

【问题讨论】:

  • 使用类型加速键[Pscustomobject]@{..} | Export-..,否则指定为有序

标签: powershell csv


【解决方案1】:

Hash Tables 默认不是ordered,除非您以这种方式创建它们:

New-Object PSObject -Property (
[ordered]@{
    Product = 1
    Stockcode = 2
    QuantityCounted = 3
})

此外,如果您运行的不是旧版本的 PowerShell,则可以使用 PSCustomObject 类型的加速器创建具有有序属性的对象。这比New-Object 更高效、更直接。

在 PowerShell 4.0 中添加了 [pscustomobject] 类型加速器。

[pscustomobject]@{
    Product = 1
    Stockcode = 2
    QuantityCounted = 3
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多