<p>How do I ask PowerShell where something is?  </p>

<p>For instance, "which notepad" and it returns the directory where the notepad.exe is run from according to the current paths.</p>

[{'answer_id': 63831, 'author': 'Nicholas', 'author_id': 8054, 'author_profile': 'https://Stackoverflow.com/users/8054', 'pm_score': 2, 'selected': False, 'text': '<p>This seems to do what you want (I found it on <a href="http://huddledmasses.org/powershell-find-path/" rel="nofollow noreferrer">http://huddledmasses.org/powershell-find-path/</a>):</p>\n\n<pre><code>Function Find-Path($Path, [switch]$All = $false, [Microsoft.PowerShell.Commands.TestPathType]$type = "Any")\n## You could comment out the function stuff and use it as a script instead, with this line:\n#param($Path, [switch]$All = $false, [Microsoft.PowerShell.Commands.TestPathType]$type = "Any")\n   if($(Test-Path $Path -Type $type)) {\n      return $path\n   } else {\n      [string[]]$paths = @($pwd);\n      $paths += "$pwd;$env:path".split(";")\n\n      $paths = Join-Path $paths $(Split-Path $Path -leaf) | ? { Test-Path $_ -Type $type }\n      if($paths.Length -gt 0) {\n         if($All) {\n            return $paths;\n         } else {\n            return $paths[0]\n         }\n      }\n   }\n   throw "Couldn\'t find a matching path of type $type"\n}\nSet-Alias find Find-Path\n</code></pre>\n'}, {'answer_id': 63835, 'author': 'tzot', 'author_id': 6899, 'author_profile': 'https://Stackoverflow.com/users/6899', 'pm_score': 2, 'selected': False, 'text': '<p>Check this <a href="http://blog.stangroome.com/2007/09/06/my-manwich-powershell-which" rel="nofollow noreferrer">PowerShell Which</a>.</p>\n\n<p>The code provided there suggests this:</p>\n\n<pre class="lang-c prettyprint-override"><code>($Env:Path).Split(";") | Get-ChildItem -filter notepad.exe\n</code></pre>\n'}, {'answer_id': 63891, 'author': 'David Mohundro', 'author_id': 4570, 'author_profile': 'https://Stackoverflow.com/users/4570', 'pm_score': 7, 'selected': False, 'text': "<p>I usually just type:</p>\n\n<pre><code>gcm notepad\n</code></pre>\n\n<p>or</p>\n\n<pre><code>gcm note*\n</code></pre>\n\n<p>gcm is the default alias for Get-Command.</p>\n\n<p>On my system, gcm note* outputs:</p>\n\n<pre><code>[27] » gcm note*\n\nCommandType     Name                                                     Definition\n-----------     ----                                                     ----------\nApplication     notepad.exe                                              C:\\WINDOWS\\notepad.exe\nApplication     notepad.exe                                              C:\\WINDOWS\\system32\\notepad.exe\nApplication     Notepad2.exe                                             C:\\Utils\\Notepad2.exe\nApplication     Notepad2.ini                                             C:\\Utils\\Notepad2.ini\n</code></pre>\n\n<p>You get the directory and the command that matches what you're looking for.</p>\n"}, {'answer_id': 65148, 'author': 'halr9000', 'author_id': 6637, 'author_profile': 'https://Stackoverflow.com/users/6637', 'pm_score': 10, 'selected': True, 'text': '<p>The very first alias I made once I started customizing my profile in PowerShell was \'which\'.</p>\n\n<pre class="lang-sh prettyprint-override"><code>New-Alias which get-command\n</code></pre>\n\n<p>To add this to your profile, type this:</p>\n\n<pre class="lang-sh prettyprint-override"><code>"`nNew-Alias which get-command" | add-content $profile\n</code></pre>\n\n<p>The `n at the start of the last line is to ensure it will start as a new line.</p>\n'}, {'answer_id': 8484635, 'author': 'Anonymous', 'author_id': 1095069, 'author_profile': 'https://Stackoverflow.com/users/1095069', 'pm_score': 2, 'selected': False, 'text': '<p>Try the <a href="http://ss64.com/nt/where.html" rel="nofollow noreferrer"><code>where</code></a> command on Windows 2003 or later (or Windows 2000/XP if you\'ve installed a Resource Kit). </p>\n\n<p>BTW, this received more answers in other questions:</p>\n\n<p><a href="https://stackoverflow.com/questions/304319/is-there-an-equivalent-of-which-on-windows">Is there an equivalent of \'which\' on Windows?</a></p>\n\n<p><a href="https://superuser.com/questions/34492/powershell-equivalent-to-unix-which-command">PowerShell equivalent to Unix <code>which</code> command?</a></p>\n'}, {'answer_id': 16949127, 'author': 'petrsnd', 'author_id': 246826, 'author_profile': 'https://Stackoverflow.com/users/246826', 'pm_score': 8, 'selected': False, 'text': "<p>Here is an actual *nix equivalent, i.e. it gives *nix-style output.</p>\n\n<pre><code>Get-Command &lt;your command&gt; | Select-Object -ExpandProperty Definition\n</code></pre>\n\n<p>Just replace  with whatever you're looking for.</p>\n\n<pre><code>PS C:\\&gt; Get-Command notepad.exe | Select-Object -ExpandProperty Definition\nC:\\Windows\\system32\\notepad.exe\n</code></pre>\n\n<p>When you add it to your profile, you will want to use a function rather than an alias because you can't use aliases with pipes:</p>\n\n<pre><code>function which($name)\n{\n    Get-Command $name | Select-Object -ExpandProperty Definition\n}\n</code></pre>\n\n<p>Now, when you reload your profile you can do this:</p>\n\n<pre><code>PS C:\\&gt; which notepad\nC:\\Windows\\system32\\notepad.exe\n</code></pre>\n"}, {'answer_id': 20728713, 'author': 'Jerome', 'author_id': 1503073, 'author_profile': 'https://Stackoverflow.com/users/1503073', 'pm_score': 0, 'selected': False, 'text': '<p>Use:</p>\n\n<pre><code>function Which([string] $cmd) {\n  $path = (($Env:Path).Split(";") | Select -uniq | Where { $_.Length } | Where { Test-Path $_ } | Get-ChildItem -filter $cmd).FullName\n  if ($path) { $path.ToString() }\n}\n\n# Check if Chocolatey is installed\nif (Which(\'cinst.bat\')) {\n  Write-Host "yes"\n} else {\n  Write-Host "no"\n}\n</code></pre>\n\n<p>Or this version, calling the original where command.</p>\n\n<p>This version also works better, because it is not limited to bat files:</p>\n\n<pre><code>function which([string] $cmd) {\n  $where = iex $(Join-Path $env:SystemRoot "System32\\where.exe $cmd 2&gt;&amp;1")\n  $first = $($where -split \'[\\r\\n]\')\n  if ($first.getType().BaseType.Name -eq \'Array\') {\n    $first = $first[0]\n  }\n  if (Test-Path $first) {\n    $first\n  }\n}\n\n# Check if Curl is installed\nif (which(\'curl\')) {\n  echo \'yes\'\n} else {\n  echo \'no\'\n}\n</code></pre>\n'}, {'answer_id': 22776692, 'author': 'thesqldev', 'author_id': 3483572, 'author_profile': 'https://Stackoverflow.com/users/3483572', 'pm_score': 5, 'selected': False, 'text': '<p><strong>Try this example:</strong></p>\n\n<pre><code>(Get-Command notepad.exe).Path\n</code></pre>\n'}, {'answer_id': 33754315, 'author': 'VortiFred', 'author_id': 5571459, 'author_profile': 'https://Stackoverflow.com/users/5571459', 'pm_score': 4, 'selected': False, 'text': '<p>My proposition for the Which function:</p>\n\n<pre><code>function which($cmd) { get-command $cmd | % { $_.Path } }\n\nPS C:\\&gt; which devcon\n\nC:\\local\\code\\bin\\devcon.exe\n</code></pre>\n'}, {'answer_id': 43299243, 'author': 'Chris F Carroll', 'author_id': 550314, 'author_profile': 'https://Stackoverflow.com/users/550314', 'pm_score': 3, 'selected': False, 'text': '<p>A quick-and-dirty match to Unix <code>which</code> is</p>\n\n<pre><code>New-Alias which where.exe\n</code></pre>\n\n<p>But it returns multiple lines if they exist so then it becomes</p>\n\n<pre><code>function which {where.exe command | select -first 1}\n</code></pre>\n'}, {'answer_id': 43354653, 'author': 'js2010', 'author_id': 6654942, 'author_profile': 'https://Stackoverflow.com/users/6654942', 'pm_score': 3, 'selected': False, 'text': '<p>I like <code>Get-Command | Format-List</code>, or shorter, using aliases for the two and only for <code>powershell.exe</code>:</p>\n<pre><code>gcm powershell | fl\n</code></pre>\n<p>You can find aliases like this:</p>\n<pre><code>alias -definition Format-List\n</code></pre>\n<p>Tab completion works with <code>gcm</code>.</p>\n<p>To have tab list all options at once:</p>\n<pre><code>set-psreadlineoption -editmode emacs\n</code></pre>\n'}, {'answer_id': 49116772, 'author': 'Jeff Zeitlin', 'author_id': 6083222, 'author_profile': 'https://Stackoverflow.com/users/6083222', 'pm_score': 1, 'selected': False, 'text': '<p>I have this <code>which</code> advanced function in my PowerShell profile:</p>\n<pre><code>    function which {\n    &lt;#\n    .SYNOPSIS\n    Identifies the source of a PowerShell command.\n    .DESCRIPTION\n    Identifies the source of a PowerShell command. External commands (Applications) are identified by the path to the executable\n    (which must be in the system PATH); cmdlets and functions are identified as such and the name of the module they are defined in\n    provided; aliases are expanded and the source of the alias definition is returned.\n    .INPUTS\n    No inputs; you cannot pipe data to this function.\n    .OUTPUTS\n    .PARAMETER Name\n    The name of the command to be identified.\n    .EXAMPLE\n    PS C:\\Users\\Smith\\Documents&gt; which Get-Command\n    \n    Get-Command: Cmdlet in module Microsoft.PowerShell.Core\n    \n    (Identifies type and source of command)\n    .EXAMPLE\n    PS C:\\Users\\Smith\\Documents&gt; which notepad\n    \n    C:\\WINDOWS\\SYSTEM32\\notepad.exe\n    \n    (Indicates the full path of the executable)\n    #&gt;\n        param(\n        [String]$name\n        )\n    \n        $cmd = Get-Command $name\n        $redirect = $null\n        switch ($cmd.CommandType) {\n            &quot;Alias&quot;          { &quot;{0}: Alias for ({1})&quot; -f $cmd.Name, (. { which $cmd.Definition } ) }\n            &quot;Application&quot;    { $cmd.Source }\n            &quot;Cmdlet&quot;         { &quot;{0}: {1} {2}&quot; -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) { &quot;in module {0}&quot; -f $cmd.Source} else { &quot;from unspecified source&quot; } } ) }\n            &quot;Function&quot;       { &quot;{0}: {1} {2}&quot; -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) { &quot;in module {0}&quot; -f $cmd.Source} else { &quot;from unspecified source&quot; } } ) }\n            &quot;Workflow&quot;       { &quot;{0}: {1} {2}&quot; -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) { &quot;in module {0}&quot; -f $cmd.Source} else { &quot;from unspecified source&quot; } } ) }\n            &quot;ExternalScript&quot; { $cmd.Source }\n            default          { $cmd }\n        }\n    }\n</code></pre>\n'}, {'answer_id': 62127237, 'author': 'Amin', 'author_id': 7910299, 'author_profile': 'https://Stackoverflow.com/users/7910299', 'pm_score': 2, 'selected': False, 'text': '<p>If you want a comamnd that both accepts input from pipeline or as paramater, you should try this:</p>\n\n<pre class="lang-bsh prettyprint-override"><code>function which($name) {\n    if ($name) { $input = $name }\n    Get-Command $input | Select-Object -ExpandProperty Path\n}\n</code></pre>\n\n<p>copy-paste the command to your profile (<code>notepad $profile</code>).</p>\n\n<p>Examples:</p>\n\n<pre class="lang-bsh prettyprint-override"><code>❯ echo clang.exe | which\nC:\\Program Files\\LLVM\\bin\\clang.exe\n\n❯ which clang.exe\nC:\\Program Files\\LLVM\\bin\\clang.exe\n</code></pre>\n'}, {'answer_id': 62995766, 'author': 'George Ogden', 'author_id': 12103577, 'author_profile': 'https://Stackoverflow.com/users/12103577', 'pm_score': 0, 'selected': False, 'text': '<p>You can install the <code>which</code> command from <a href="https://goprogram.co.uk/software/commands" rel="nofollow noreferrer">https://goprogram.co.uk/software/commands</a>, along with all of the other UNIX commands.</p>\n'}, {'answer_id': 69868490, 'author': 'blenderfreaky', 'author_id': 7869744, 'author_profile': 'https://Stackoverflow.com/users/7869744', 'pm_score': 0, 'selected': False, 'text': '<p>If you have <a href="https://github.com/ScoopInstaller/Scoop" rel="nofollow noreferrer">scoop</a> you can install a direct clone of which:</p>\n<pre><code>scoop install which\nwhich notepad\n</code></pre>\n'}, {'answer_id': 71627654, 'author': 'rayiik', 'author_id': 14073060, 'author_profile': 'https://Stackoverflow.com/users/14073060', 'pm_score': 0, 'selected': False, 'text': '<p>There also always the option of using which. there are actually three ways to access which from Windows powershell, the first (not necessarily the best) wsl -e which command (this requires installation of windows subsystem for Linux and a running distro). B. <a href="http://gnuwin32.sourceforge.net/packages.html" rel="nofollow noreferrer">gnuwin32</a> which is a port of several gnu binaries in .exe format as standle alone bundled lanunchers option three, install <a href="https://www.msys2.org/" rel="nofollow noreferrer">msys2</a> (cross compiler platform) if you go where it installed in /usr/bin you\'ll find many many gnu utils that are more up-to-date. most of them work as stand alone exe and can be copied from the bin folder to your home drive somewhere amd added to your PATH.</p>\n'}, {'answer_id': 71627691, 'author': 'rayiik', 'author_id': 14073060, 'author_profile': 'https://Stackoverflow.com/users/14073060', 'pm_score': -1, 'selected': False, 'text': '<p>There also always the option of using which. there are actually three ways to access which from Windows powershell</p>\n<ul>\n<li>The first, (though not the best) is wsl(windows subsystem for linux)</li>\n</ul>\n<pre class="lang-sh prettyprint-override"><code>wsl -e which command \n</code></pre>\n<p>This requires installation of windows subsystem for Linux and a running distro.</p>\n<ul>\n<li><p>Next is <a href="http://gnuwin32.sourceforge.net/packages.html" rel="nofollow noreferrer">gnuwin32</a> which is a port of several gnu binaries in .exe format as standle alone bundled lanunchers</p>\n</li>\n<li><p>Third, install <a href="https://www.msys2.org/" rel="nofollow noreferrer">msys2</a> (cross compiler platform) if you go where it installed in /usr/bin you\'ll find many many gnu utils that are more up-to-date. most of them work as stand alone exe and can be copied from the bin folder to your home drive somewhere amd added to your PATH.</p>\n</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/63805', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1220/']