powershell - cd 程序文件错误:找不到位置参数
问题描述
PS C:\> cd Program Files
当我发出这个命令时,我不知道为什么,但它不接受Program Files
. 相同的命令在cmd
.
这是它显示的错误:
Set-Location : A positional parameter cannot be found that accepts argument 'Files'.
At line:1 char:1
+ cd Program Files
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-Location], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
解决方案
正如Maximilian Burszley 的有用答案中所述,Program Files
被解析为两个参数,因为空格用于分隔命令行参数。
您的使用尝试cd Program Files
可能会受到cmd.exe
(遗留命令提示符)的启发,这种语法确实有效;然而,即使在那里,它在概念上也违反了通常的参数解析规则。
因此,您需要使用一种引用形式才能将包含空格的值作为单个参数传递。
你有几个选项来实现这个引用:
使用文字值,即应该按原样使用的值
'...'
。在要嵌入
"..."
变量引用(例如$HOME
)或子表达式(例如$(Get-Date)
.用于
`
引用(转义)单个字符。
因此,您可以使用以下任何一种:
cd 'Program Files'
cd "Program Files" # as there are no $-prefixed tokens, the same as 'Program Files'
cd Program` Files # `-escape just the space char.
此外,您可以使用tab-completion将包含空格的路径的(无空格)前缀扩展为其完整形式,并隐式应用引用。
例如,如果你在里面C:\
并且你输入:
cd Program<tab>
PowerShell 自动完成以下命令:
cd '.\Program Files\'
请注意Program Files
(以及.\
引用当前目录和尾随\
以指示目录。)是如何自动单引号引起来的。
推荐阅读
- macos - 当屏幕被锁定/关闭时,cmd.Run() 在 Macos 上的 golang 中永远挂起
- python - 如何修复python中的“字符串索引超出范围错误”。在下面找到错误消息
- mysql - Kotlin Exposed 中的 MySQL 显示表的等价物是什么
- c++ - 我想从文本文件中获取信息并将该信息分配给类对象
- mysql - Laravel DB::raw 读取 where 子句中的第二个参数是一个字符串,而不是另一个表中的动态列
- c - 如何替换char数组中的单个字符?
- asp.net - 添加委托处理程序时 HttpContext.Current 为 null
- sublimetext3 - SublimeText 和 ignore_vcs_packages 设置
- python - Django:为什么我必须在我的 AppConfig 中导入信号(它似乎没有工作)
- javascript - 如何在 React-Quill 中设置字符长度