是否可以将PowerShell命令行拆分为多行?

在Visual Basic中,我可以在下一行中使用下划线(_)来继续命令。


当前回答

我刚刚发现,在后勾号和换行符之间一定没有任何字符。即使是空格也会导致命令不起作用。

其他回答

在powershell中有很多方法来继续一行,用管道,括号,圆括号,操作符,点,甚至用逗号。这里有一个关于它的博客:https://get-powershellblog.blogspot.com/2017/07/bye-bye-backtick-natural-line.html

你也可以在foreach和if语句之后继续。

使用分号;分开命令 在任何反斜杠上替换双反斜杠\\。 使用“'传递安全地址到交换机命令,如“'PATH'”。

这个ps1命令安装地区pfx证书。

powershell -Command "$pfxPassword = ConvertTo-SecureString -String "12345678" -Force -AsPlainText ; Import-PfxCertificate -FilePath "'C:\\Program Files\\VpnManagement\\resources\\assets\\cert\\localhost.pfx'" Cert:\\LocalMachine\\My -Password $pfxPassword ; Import-PfxCertificate -FilePath "'C:\\Program Files\\VpnManagement\\resources\\assets\\cert\\localhost.pfx'" Cert:\\LocalMachine\\Root -Password $pfxPassword"

如果你想把字符串分成多行,你可以使用“+”。例如:

$header =    "Make," +

             "ComputerName," +

             "Model," +

             "Windows Version"

看起来就像:

$header = "Make,ComputerName,Model,Windows Version"

在PowerShell和PowerShell ISE中,也可以在每行末尾使用Shift + Enter进行多行编辑(而不是标准的反引号')。

只要在这里加上一个极端情况。这可能会帮你节省5分钟。如果你使用一连串的动作,你需要在行末加上“。”,然后在“'”后面留下一个空格(反勾号)。这是我很不容易才发现的。

$yourString = "HELLO world! POWERSHELL!". `
                  Replace("HELLO", "Hello"). `
                  Replace("POWERSHELL", "Powershell")