Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I alias a PowerShell script to be called from prompt?

Tags:

powershell

cmd

I have a PowerShell script that opens all files and programs related to my development environment. Rather than typing

> &.\mysetup.ps1

is there some way I can alias it to just type

> gosetup

?

Ideally I'll use this functionality for other PowerShell scripts as well! For reference, I want to run these commands from the Git PowerShell window.

like image 783
flacnut Avatar asked Nov 14 '12 00:11

flacnut


1 Answers

  • First you need to set up your PowerShell profile, if you haven't done so already.
  • Create a directory where you want to keep your aliased scripts (I use C:\ps).
  • In your profile, add the following lines:

    $ps_script_dir = "C:\path\to\scripts"
    
    New-Alias <alias name> $ps_script_dir\<script name>
    
  • Open a new shell, and you should be able to call your script by its alias.

Update / Additional Information

  • The following is in response to the OP's comment, below.

If you want to simply assign a one-liner, or a short snip of code to an alias, you don't need to put it into a script file, as described above, you can simply create a function in your profile:

function someUsefulOneliner
{
    # Your one-liner goes here
}

set-item -path alias:<alias name> -value someUsefulOneliner
like image 75
David Avatar answered Oct 19 '22 02:10

David