从 power shell 中没有列名的平面文件中选择列

Selecting columns from flat file in power shell with no column name

我是 power shell 的新手,我有以下格式(pipe 分隔符)没有 名称:

01|1|06/28/2017 00:00:00|06/28/2017 00:00:00

我想从这种格式中选择第三列或任何列,我试过下面的代码:

$columns=(Get-Content $filepath | Out-String | select -Skip 2 -First 1).Split("|") 

但是它不起作用,请问有人能帮忙吗。

使用 Import-CSV 并指定 -Header-Delimiter;这样,您将获得一个结构 (PSCustomObject[]),其中包含您可以直接且有意义地引用的属性。例如,

$EntryList = Import-CSV -Path $FilePath -Header ID,Type,StartTime,EndTime -Delimiter '|'

为您提供一个 PSCustomObjects 数组,其中每个对象都有指定的字段。然后,您可以(例如)参考 $EntryList[$n].ID$EntryList[$n].StartTime 等。