對 C:\Program Files 中的資料夾使用 Powershell 壓縮存檔

對 C:\Program Files 中的資料夾使用 Powershell 壓縮存檔

嘗試C:\Program Files使用 Powershell 壓縮資料夾,但出現以下錯誤。如果有人可以提供協助,我們將不勝感激。

命令:

powershell.exe -nologo -noprofile -command Compress-Archive -Path "C:\Program Files\avr\cache.dat" -Destinationpath "C:\Program Files\avr\bur\"

錯誤:

Compress-Archive : A positional parameter cannot be found that accepts argument 'Files\avr\cache.dat'.
At line:1 char:1
+ Compress-Archive -Path C:\Program Files\avr\cache.dat -Destina ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo         : InvalidArgument: (:) [Compress-Archive], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Compress-Archive

答案1

閱讀並遵循powershell /?(內建幫助):

-Command
To write a string that runs a Windows PowerShell command, use the format:
    "& {<command>}"
where the quotation marks indicate a string and the invoke operator (&)
causes the command to be executed.

使用

powershell.exe -nologo -noprofile -command "& {Compress-Archive -Path 'C:\Program Files\avr\cache.dat' -Destinationpath 'C:\Program Files\avr\bur\'}"

答案2

命令的 Powershell 部分將原樣在 Powershell 介面中運行。但如果從命令提示字元運行它,則需要用反斜線轉義引號。像這樣:

powershell.exe -nologo -noprofile -command Compress-Archive -Path \"C:\Program Files\avr\cache.dat\" -Destinationpath \"C:\Program Files\avr\bur\\"

相關內容