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

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


当前回答

在windows终端(powershell配置文件),我可以简单地单击Shift-Enter对我来说很好。

PS C:\xxx2021> Get-ChildItem -Include *remote* -Recurse |
>> Sort-Object -Property LastWriteTime -Descending  |
>> Select-Object LastWriteTime, Name -First 25

LastWriteTime        Name
-------------        ----
12/5/2021 5:04:02 PM remote-control-car-BatteryPack-Number-2021-12-03.pdf

PS C:\xxx2021>enter code here

其他回答

$scriptBlock = [Scriptblock]::Create(@'
  echo 'before'
  ipconfig /all
  echo 'after'
'@)

Invoke-Command -ComputerName AD01 -ScriptBlock $scriptBlock

源 不要使用反引号

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

这是一个老帖子,所以这是现代的方法。

如果您不使用遗留的powershell,那么延续行最干净的方法是在行开始处使用管道。

注意:该命令不会将某些行注释掉。这在命令行中非常有用。

> Get-ChildItem -path 'c:\' -Depth 1                 
    | Sort-Object LastWriteTime                       
    # | Sort-Object Length -Descending                   
    | Select-Object -First 3 -Skip 3                   
    | Foreach-Object {                                 
      $_.Name, $_.Length | Join-String -Separator ' = '                                                       
    }                                                  

输出:

explorer.exe = 4826160                                 
procexp.old.exe = 2925760                              
RtlExUpd.dll = 2839488                                 

Windows Powershell(版本< 6)

不幸的是,windows powershell不支持它。上面链接了一堆替代方案。你可以完全删除反勾号:2017/07/ byye - byye -backtick-natural-line

在多行中使用'字符分隔命令

扩展一下克里斯托巴利托的回答:

我假设您谈论的是命令行——如果是在脚本中,那么新行>充当命令分隔符。 在命令行中,使用分号';'

例如:

在命令行上签署PowerShell脚本。没有换行。

powershell -Command "&{$cert=Get-ChildItem –Path cert:\CurrentUser\my -codeSigningCert ; Set-AuthenticodeSignature -filepath Z:\test.ps1 -Cert $cert}