如何在PowerShell中以编程方式写入当前提示行

3

是否有可能将文本发送到当前提示行?类似以下内容:

PS[10]> Get-Location
c:/
PS[11]> Write-PromptLine "This text has been written by Powershell"
PS[12]> This text has been written by Powershell|
# | denotes the caret
1个回答

5
您可以使用 SendKeys.SendWait 方法:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.SendKeys]::SendWait("This text has been written by Powershell")

我希望$host对象能够提供那个功能,但我会稍后尝试你的方法是否适用于我。谢谢。 - guillermooo
我在$host中找不到任何东西(这是我尝试的第一件事),但SendKeys可以实现你要求的功能。你具体想要实现什么? - Paolo Tedesco
将最后一个命令行发送到编辑器,阻止 shell 执行,编辑命令并保存到临时文件中,关闭编辑器并将结果发送到 PS 提示行,而无需立即执行。虽然现在无法测试,但您的解决方案应该有效。 - guillermooo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接