Powershell:仅导出字符串值

时间:2015-06-03 20:24:09

标签: powershell

我有一个脚本,它读取文本文件,通过select-string捕获特定的字符串值,然后写入csv文件。我遇到的问题是它会自动添加我不想要的其他列:

IgnoreCase LineNumber行文件名路径模式上下文匹配

我想要的字符串值在下 - 如何仅输出我捕获的字符串值?

1 个答案:

答案 0 :(得分:1)

使用Select-Object -ExpandPropertySelect-String返回的MatchInfo对象中抓取一个属性值:

$MatchedStrings = Get-Item C:\Windows\system32\WindowsPowerShell\v1.0\en-US\about_Foreach.help.txt |select-string "operator"
$MatchedStrings | Select-Object -ExpandProperty Line | Out-File C:\myStrings.txt

如果要输出到单列CSV文件并保留Line标题,请改用Select-Object -Property

$MatchedStrings | Select-Object -Property Line | Export-Csv C:\strings.csv -NoTypeInformation