powershell - 如何通过管道将目录名称传递给 Set-Location cmdlet?
问题描述
我正在尝试查找某个命令的目录,然后将当前目录更改为找到的目录。
下面的命令给了我一个目录:
Get-Item -Path $(where.exe cmd.exe) | Select -Property Directory
Directory
---------
C:\Windows\System32
现在我尝试将此目录通过管道传输到 Set-Location cmdlet,但收到错误消息:
Get-Item -Path $(where.exe cmd.exe) | Select -Property Directory | Set-Location
Set-Location : Cannot find drive. A drive with the name '@{Directory=C' does not exist.
At line:1 char:67
+ ... -Path $(where.exe cmd.exe) | Select -Property Directory |Set-Location
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (@{Directory=C:String) [Set-Location], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
我怎样才能做到这一点?请帮忙。
解决方案
Get-Item -Path $(where.exe cmd.exe) | Select -ExpandProperty Directory | Set-Location
这个值得解释一下。
在文件系统中使用时,根据情况Get-Item
返回一个FileInfo
或DirectoryInfo
对象。他们都有Directory
财产,到目前为止一切都很好。
Select-Object SomeProperty
默认情况下,不会为您提供任何值SomeProperty
。它为您提供了一个新对象 (a PSObject
),它SomeProperty
是唯一的成员。这是为了支持你这样做的情况Select-Object SomeProperty, SomeOtherProperty
。
如果您想要输入对象的单个属性的原始值,则必须明确说明。就是Select-Object -ExpandProperty SomeProperty
这样。(推论:你不能这样做Select-Object -ExpandProperty SomeProperty, SomeOtherProperty
。)
Set-Location
需要一个字符串。当您传递其他内容时,例如 a PSObject
,它会将其转换为字符串。并且 a 的字符串表示形式PSObject
不能用作路径,即使它是PSObject
仅包含路径的 a。
另一种获取原始值的方法 - 直接属性访问 - 也可以(这可能是您从一开始就想到的):
(Get-Item -Path $(where.exe cmd.exe)).Directory | Set-Location
并且由于我们已经确定Set-Location
需要一个字符串,所以这也有效:
$(where.exe cmd.exe) | Split-Path -Parent | Set-Location
顺便说一句,有一种方便的方法可以用where.exe
PowerShell 原生的东西替换有点尴尬的调用 - Get-Command
:
Set-Location ((Get-Command cmd).Source | Split-Path -Parent)
推荐阅读
- sql - 编写查询以打印一年中的最后一个日期,输入将是任何日期
- data-science - 在特征选择期间要删除哪些特征
- python - 为什么 python 不为其关联的 --hhelp 使用自己的 argparse 库?
- matlab - 如何并行运行代码?(通过VISA和RS232与设备通信
- javascript - rxjs 合并 http 响应
- c++ - 为什么 hashmap 的名称中有“hash”?
- electron - 为什么我的 Echarts 用 Electron 配置的 index.html 不起作用?
- python - 基于现有列创建新列。使用 1 或 0
- r - 跨多列对变量进行排序和排名
- c++ - 为什么 cpp 中的 size_t 被命名为“size_t”?