Skip to content

Instantly share code, notes, and snippets.

@codingoutloud
Last active December 24, 2015 10:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingoutloud/6782961 to your computer and use it in GitHub Desktop.
Save codingoutloud/6782961 to your computer and use it in GitHub Desktop.
Implements the Unix shell 'which' command using PowerShell
<#
Implements the Unix shell 'which' command using PowerShell.
original: https://gist.github.com/codingoutloud/6782961
#>
function WhichOnes
{
if ($args.Count -eq 0)
{
# obviously use of 'which' in usage text is a hack (since it is chosen to match the alias)
Write-Host "usage: which [cmd1] [cmd2] ..."
}
else
{
Get-Command $args -All -ErrorAction SilentlyContinue | Format-Table Definition
}
}
New-Alias which WhichOnes
@codingoutloud
Copy link
Author

PowerShell 'which'

original is here

Implements the Unix shell 'which' command using PowerShell.

Usage

_which grep_ -- lists paths to any existing grep commands available.

To add to your PowerShell environment

  • Open a PowerShell command prompt
  • Edit your profile startup script using _notepad $profile_ (see Troubleshooting below if you get an error)
  • Paste in the contents of this gist
  • Save the file
  • Close PowerShell and start a new PowerShell shell session
  • ... and it should work.

Troubleshooting

  • You need to open a new PowerShell shell in order for the script to be run automatically. So this will be a problem only the first time after you've created the script.
  • If notepad complains that the path does not exist during the "notepad $profile" step, run the following command first: _mkdir $env:USERPROFILE\WindowsPowerShell_
  • If PowerShell complains about the script with a message similar to Microsoft.PowerShell_profile.ps1 cannot be loaded because running scripts is disabled on this system. then your PowerShell environment does not have sufficient permissions to execute unsigned scripts locally. You can fix this by running a PowerShell shell as adminstrator and then executing the command Set-ExecutionPolicy RemoteSigned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment