替换PowerShell中的$ variable输出

时间:2015-12-25 10:50:46

标签: powershell

使用函数我得到这样的输出:

User                                                                                                         
----                                                                                                         
Domain\Username

但是当我试图替换输出中的“Domain”时

$f -replace 'Domain\\'

我得到了这个:

@{User=Username}

我正在使用不良功能或其他是出错的?

2 个答案:

答案 0 :(得分:2)

试试这个:

$f.User -replace 'Domain\\', ''

而不是 -

$f -replace 'Domain\'

您变量的输出显示答案。您有一个user属性的对象应该是您认为正在处理的字符串。

答案 1 :(得分:1)

您的输出是具有属性User的对象。如果您只是在对象上使用-replace,则基本上将对象转换为字符串(@{User=Domain\Username})并替换该字符串中的Domain\。使用属性User上的运算符从该属性的值中删除子字符串:

$f.User -replace 'Domain\\'