约 1281 字

约 6 分钟

#PowerShell 的 Import-Csv 命令

Import-Csv [-Path] <String[]> [-Delimiter <Char>] [-Header <String[]>] [-Encoding <Encoding>]
Import-Csv -LiteralPath <String[]> [-Delimiter <Char>] [-Header <String[]>] [-Encoding <Encoding>]

功能

读取 CSV 文件。每条记录成为一个自定义对象,列名成为属性名,字段值以字符串读入。

类型

Cmdlet,属于 Microsoft.PowerShell.Utility。

参数

  • -Path <String[]> 用来指定文件路径,支持通配符。-LiteralPath 按路径原样查找。
  • -Delimiter <Char> 用来指定分隔符,默认为逗号。
  • -Header <String[]> 用来为没有表头的文件提供列名。使用这个参数时,第一行会作为数据读取。
  • -Encoding <Encoding> 用来指定读取文件时使用的编码。

#示例

导入后按数值筛选

在空练习目录中创建 scores.csv 并读取:

@'
Name,Score
Lin,92
Chen,9
'@ | Set-Content -LiteralPath ./scores.csv -Encoding utf8

$rows = Import-Csv -LiteralPath ./scores.csv -Encoding utf8
$rows | Where-Object { [int]$_.Score -ge 80 } | Select-Object -ExpandProperty Name
Lin

[int]$_.Score 将分数转换为整数,再与 80 比较。

#推荐阅读

更新: 2026/11/27

作者: planc

创建: 2026/11/27