2017-10-09 100 views
1

我想在PowerShell中爲where.exe提供相同的功能。在PowerShell中執行`where.exe`

我試着找到一個替代標準的可執行程序,但找不到任何東西的cmdlet。

所以默認情況下,我導致試圖在PowerShell中執行where命令,但是映射到該命令的別名。

 
PS C:\> Get-Command where 

CommandType  Name           Version Source 
-----------  ----           ------- ------ 
Alias   where -> Where-Object 

是否有在PowerShell中執行where的方式還是有更好的方法來達到同樣的效果?

+1

'Get-Command notepad -CommandType Application' – PetSerAl

+0

Get-Command不適用於搜索 –

+1

您可以在powershell中執行where.exe(或任何可執行文件),例如:'&where.exe',例如'&where.exe/R C:\ Windows notepad.exe' –

回答

2

我不會刪除現有的where別名,因爲這會導致您可能會使用失敗的腳本。相反,你可以定義一個新的別名爲它:

Set-Alias -Name "wherecli" -Value "where.exe" 

然後你就可以使用wherecli

+0

啊,是一個很好的解決方法!是否可以爲給定的會話設置別名?謝謝 –

+0

該cmdlet僅爲當前會話設置別名;-) –

+1

剛剛看到,Doh! –

2

我找到了一個解決方法只用Get-ChildItemWhere-Object但這似乎慢where.exe但想到我會列出它取決於你想搜索的內容可能是有用的。

PS C:\> Get-ChildItem -Path "c:\Windows\" -Recurse -File | Where-Object {$_.Name -eq "notepad.exe"} 
+0

使用過濾器參數應該比管道到哪裏對象快: 'Get-ChildItem -Path「c:\ Windows \」-Recurse -File -Filter'notepad.exe'' 雖然不知道它如何比較。exe –

1

當然,如果你不想刪除where別名指向Where-Object,您可以鍵入.exe(輸入.,然後按TAB鍵就夠了),如:

where.exe notepad 

否則,您是否使用-All參數(PowerShell 3及更高版本)嘗試Get-Command(別名gcm)?例如:

gcm notepad -All 

我的機器上,給出了:

CommandType  Name            Version Source 
-----------  ----            ------- ------ 
Application  notepad.exe          10.0.10... C:\Windows\system32\notepad.exe 
Application  notepad.exe          10.0.10... C:\Windows\notepad.exe

而且它會發現PowerShell的東西爲好,在這給例如gcm where -All

CommandType  Name            Version Source 
-----------  ----            ------- ------ 
Alias   where -> Where-Object 
Application  where.exe           10.0.10... C:\Windows\system32\where.exe

如果您要想要刪除「標準」別名,請使用Remove-Item alias:where(如果您希望在PowerShell的每次啓動時完成,可以通過放入配置文件)。

+0

:thumbsup:注意你可以通過包含'.exe'擴展名來執行'where' –