拆分选择字符串

时间:2014-07-20 02:08:40

标签: powershell split

鉴于此变量

$foo = help | Select-String powershell

尝试拆分将失败

PS > $foo.split()
Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo]
does not contain a method named 'split'.
At line:1 char:1
+ $foo.split()
+ ~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

如何拆分此变量?

2 个答案:

答案 0 :(得分:6)

匹配行的字符串值位于MatchInfo对象的Line属性中。

$foo.Line.split()

答案 1 :(得分:0)

Split是一个String方法,因此必须在字符串上调用它。

[string] $foo = help | Select-String powershell

或者

$foo = help | Select-String powershell | Out-String