Dev*_*ris 363 unix powershell command
我如何询问PowerShell的位置?
例如,"notepad",它根据当前路径返回运行notepad.exe的目录.
hal*_*000 350
一旦我开始在PowerShell中自定义我的配置文件,我创建的第一个别名是'which'.
New-Alias which get-command
Run Code Online (Sandbox Code Playgroud)
要将此添加到您的个人资料,请输入以下内容
"`nNew-Alias which get-command" | add-content $profile
Run Code Online (Sandbox Code Playgroud)
最后一行开头的`n是确保它将作为新行开始.
pet*_*snd 152
这是一个实际的*nix等价物,即它给出了*nix风格的输出.
Get-Command <your command> | Select-Object -ExpandProperty Definition
Run Code Online (Sandbox Code Playgroud)
只需替换你想要的任何东西.
PS C:\> Get-Command notepad.exe | Select-Object -ExpandProperty Definition
C:\Windows\system32\notepad.exe
Run Code Online (Sandbox Code Playgroud)
将其添加到配置文件时,您将需要使用函数而不是别名,因为您不能对管道使用别名:
function which($name)
{
Get-Command $name | Select-Object -ExpandProperty Definition
}
Run Code Online (Sandbox Code Playgroud)
现在,当您重新加载配置文件时,您可以执行以下操作:
PS C:\> which notepad
C:\Windows\system32\notepad.exe
Run Code Online (Sandbox Code Playgroud)
Dav*_*dro 80
我通常只输入:
gcm notepad
Run Code Online (Sandbox Code Playgroud)
要么
gcm note*
Run Code Online (Sandbox Code Playgroud)
gcm是Get-Command的默认别名.
在我的系统上,gcm note*输出:
[27] » gcm note*
CommandType Name Definition
----------- ---- ----------
Application notepad.exe C:\WINDOWS\notepad.exe
Application notepad.exe C:\WINDOWS\system32\notepad.exe
Application Notepad2.exe C:\Utils\Notepad2.exe
Application Notepad2.ini C:\Utils\Notepad2.ini
Run Code Online (Sandbox Code Playgroud)
您将获得与您要查找的目录和命令相匹配的目录和命令.
小智 36
试试这个例子:
(Get-Command notepad.exe).Path
Run Code Online (Sandbox Code Playgroud)
我喜欢Get-Command | Format-List或更短地使用两者的别名,并且仅用于powershell.exe:
gcm powershell | fl
Run Code Online (Sandbox Code Playgroud)
您可以这样查找别名:
alias -definition Format-List
Run Code Online (Sandbox Code Playgroud)
制表符补全适用于gcm.
要让选项卡一次列出所有选项:
set-psreadlineoption -editmode emacs
Run Code Online (Sandbox Code Playgroud)
与 Unix 的快速而肮脏的匹配which是
New-Alias which where.exe
Run Code Online (Sandbox Code Playgroud)
但是如果它们存在,它会返回多行,所以它变成
function which {where.exe command | select -first 1}
Run Code Online (Sandbox Code Playgroud)
小智 5
我对Who函数的主张:
function which($cmd) { get-command $cmd | % { $_.Path } }
PS C:\> which devcon
C:\local\code\bin\devcon.exe
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
81888 次 |
| 最近记录: |