Get-Command 2.0 by Joel Bennett 4 years ago (modification of post by Joel Bennett view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/173"></script>download | new post
Finally, a working Which (or Whence) for PowerShell. This function supplants Get-Command and outputs the commands in the correct order — that is, the first one output is the one the shell will use.
- ## Which.ps1 - aka Get-Command
- ###################################################################################################
- ## This ought to be the same as Get-Command, except...
- ## This Which function will output the commands ...
- ## in the actual order they would be used by the shell
- ##################################################################################################
- ## Revision History
- ## 1.0 - initial release just sorted the Get-Command output by CommandType
- ## 2.0 - rewritten to take into account the order of commands in the PATH environment variable
- ##################################################################################################
- function Get-Command([string]$command) {
- begin { $Script:ErrorActionPreference = "SilentlyContinue" }
- process {
- if(!$_) { $_ = $command }
- Microsoft.PowerShell.Core\Get-Command $_ |
- sort {
- if($_.CommandType -match "ExternalScript|Application") {
- 1000 + [array]::IndexOf( (Get-Content Env:Path).Split(";"), [IO.Path]::GetDirectoryName($_.Definition) )
- } else {
- [int]$_.CommandType
- }
- }
- }
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository