在批处理中调用PowerShell时,如何在一行中回声



我在批处理中调用powershell,并引用这样的powershell命令:

Powershell -commands "&{...}"

在powershell命令中,我有一个$ counter = 0,它在for循环中更改(例如3(。现在,我想在屏幕上回声"找到3个对象"。

我尝试过在线提到的一些方法,但是它们都不适合我。可能是因为我已经使用"引用了PowerShell命令,所以我只能使用''。

我尝试了

echo(write-output) $counter objects found
echo '$counter objects found'
echo '$($counter) objects found'
$counter='0' echo '$counter objects found'

我希望看到

3 objects found 

但我得到

3
objects
found   #or
$counter objects found  #or 
$($counter) objects found

另一种方法是将$计数器的价值传递回批处理,但我认为在PowerShell中进行回声更容易。谁能帮忙?预先感谢。

我尝试了

$counter = 0
echo(write-host -NoNewline) "$counter objects found"

我得到了

0的对象

write-host之后添加参数。希望我能帮助您!

编辑:

powershell -NoP -C "$Counter=0;1..3|%{$Counter++};'{0} objects found' -f $Counter"

批次为我工作,@lotpings

发布

最新更新