16

On *nix, using BASH (etc) you ask the system where a command is located (etc) by using the 'type' shell built-in like this:

$ type cat
cat is /bin/cat

Is there an equivalent of this 'type' command in Microsoft PowerShell 2.0 ?

4 Answers 4

18

An equivalent is Get-Command.

PS C:\> Get-Command ls

CommandType     Name       Definition
-----------     ----       ----------
Alias           ls         Get-ChildItem
Application     ls.exe     D:\usr\local\wbin\ls.exe
Application     ls.exe     C:\Program Files (x86)\Git\bin\ls.exe

Windows 10 Update:

Since I've posted this answer, it appears that the behavior of Get-Command has changed. To include all results (in the style of Un*x) type), now I need to pass the -All flag, like so:

PS C:\> Get-Command -All ls

CommandType     Name                 Version    Source
-----------     ----                 -------    ------
Alias           ls -> Get-ChildItem
Application     ls.exe               0.0.0.0    C:\Program Files (x86)\Git\usr\bin\ls.exe

As noted in a comment, this doesn't include the Definition column as was the previous behavior. I can't determine a command-line argument to add the definition column, but as noted by @voutasaurus in the comment below, one can use:

PS C:\> (Get-Command -All ls).Definition
Get-ChildItem
C:\Program Files (x86)\Git\usr\bin\ls.exe

Version information for reference (I odn't have the version information associated with the original answer text, but I'm guessing that it was Windows 7):

PS C:\> [System.Environment]::OSVersion.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      15063  0
Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't work for functions defined in $profile, whereas type returns the definition of the function in bash.
I'm getting this: > Get-Command Get-Command CommandType Name Version Source ----------- ---- ------- ------ Cmdlet Get-Command 3.0.0.0 Microsoft.PowerShell.Core If you use (Get-Command Get-Command).Definition then you actually get the definition.
Thanks @voutasaurus that must have changed since the answer was posted. I've amended the answer with your suggestion.
2

Get-Command has a -ShowCommandInfo parameter that does this. It also works for functions defined in $profile :

PS C:\Users\vp937ll> Get-Command l -ShowCommandInfo

Name          : l
ModuleName    :
Module        : @{Name=}
CommandType   : Function
Definition    : Get-ChildItem | Sort-Object -Property LastWriteTime -Descending
ParameterSets : {@{Name=__AllParameterSets; IsDefault=False; Parameters=System.Management.Automation.PSObject[]}}

Comments

2
  • Get-Command seems right, aliased as gcm
  • Just pipe it to Select-Object * aliased as select *

E.g., in $profile I have a function that opens total commander from pwd (it silently kills the existing instance first)

function start-totalCommanderhere {
    $here = (Get-Location).path
    kill -n TOTALCMD64 -ErrorAction Ignore
    start "c:\totalcmd\TOTALCMD64.EXE" $here
}
Set-Alias tc start-totalCommanderhere
  • Here is all the info about the function — it however does not tell you directly that it is a function, you would need to query its proper name not alias to get that information from CommandType attribute. (than it would be same as type in bash which tells you hello_world is a function in case it is one)
▶ gcm tc | select *

HelpUri             :
ResolvedCommandName : start-totalCommanderhere
DisplayName         : tc -> start-totalCommanderhere
ReferencedCommand   : start-totalCommanderhere
ResolvedCommand     : start-totalCommanderhere
Definition          : start-totalCommanderhere
Options             : None
Description         :
OutputType          : {}
Name                : tc
CommandType         : Alias
Source              :
Version             :
Visibility          : Public
ModuleName          :
Module              :
RemotingCapability  : PowerShell
Parameters          : {}
ParameterSets       :
  • gcm <module_name> | select * gives you the path if you ask for a CommandType which is Application
▶ gcm ping | select *

HelpUri            :
FileVersionInfo    : File:             C:\Windows\system32\PING.EXE
                     InternalName:     ping.exe
                     OriginalFilename: ping.exe.mui
                     FileVersion:      10.0.19041.320 (WinBuild.160101.0800)
                     FileDescription:  TCP/IP Ping Command
                     Product:          Microsoft® Windows® Operating System
                     ProductVersion:   10.0.19041.320
                     Debug:            False
                     Patched:          False
                     PreRelease:       False
                     PrivateBuild:     False
                     SpecialBuild:     False
                     Language:         English (United States)

Path               : C:\Windows\system32\PING.EXE
Extension          : .EXE
Definition         : C:\Windows\system32\PING.EXE
Source             : C:\Windows\system32\PING.EXE
Version            : 10.0.19041.1
Visibility         : Public
OutputType         : {System.String}
Name               : PING.EXE
CommandType        : Application
ModuleName         :
Module             :
RemotingCapability : PowerShell
Parameters         :
ParameterSets      :
  • But with gcm you do not get paths for imported modules — for that you need to use Get-Module | Select-Object Name, Path aliased as gmo | select name,path
▶ gmo | select name, path

Name                            Path
----                            ----
assign-vault-keys-to-env-vars   C:\Users\Admin\Documents\workspace\work.log\kb\powershell\assign-vault-keys-to-env-vars.ps1
CimCmdlets                      C:\Program Files\PowerShell\7\Microsoft.Management.Infrastructure.CimCmdlets.dll
decode-base64                   C:\Users\Admin\Documents\workspace\work.log\kb\powershell\decode-base64.ps1
DnsClient                       C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\DnsClient\DnsClient.psd1
...

Comments

1

Since you tagged this with Shell, in addition to PowerShell's Get-Command, there's where.exe:

PS C:\> where.exe notepad
C:\Windows\System32\notepad.exe
C:\Windows\notepad.exe

The command just looks for a file with the specified name through the path:

PS C:\> where.exe readme.*
C:\Python31\README.txt
C:\Program Files (x86)\wget\README
C:\Program Files (x86)\SysinternalsSuite\readme.txt

Note that when calling this command from PowerShell, you must call it as where.exe because Where-Object is aliased to where.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.