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

fla*_*nut 5 powershell cmd

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

> &.\mysetup.ps1
Run Code Online (Sandbox Code Playgroud)

is there some way I can alias it to just type

> gosetup
Run Code Online (Sandbox Code Playgroud)

?

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.

Dav*_*vid 10

  • 首先,如果尚未设置PowerShell配置文件,则需要设置它.
  • 创建一个要保留别名脚本的目录(我使用C:\ ps).
  • 在您的个人资料中,添加以下行:

    $ps_script_dir = "C:\path\to\scripts"
    
    New-Alias <alias name> $ps_script_dir\<script name>
    
    Run Code Online (Sandbox Code Playgroud)
  • 打开一个新shell,你应该能够通过别名调用你的脚本.

更新/附加信息

  • 以下是对OP的评论的回应,如下.

如果您只想简单地将一行代码或一小段代码分配给别名,则无需将其放入脚本文件中,如上所述,您只需在配置文件中创建一个函数:

function someUsefulOneliner
{
    # Your one-liner goes here
}

set-item -path alias:<alias name> -value someUsefulOneliner
Run Code Online (Sandbox Code Playgroud)