Search code examples
powershellpowershell-cmdlet

Shorter execution of powershell script


Say I have a script to be executed in a single call, how do I do it?

Like, say I have a powershell script saved at E:\Fldr\scrpt.ps1. Now if I have to normally execute that script is PowerShell ISE then I would have to use:

& "E:\Fldr\scrpt.ps1"

and the scrpt.ps1 gets executed.

But whatI want is, when I write a word, say "exeScrpt" instead of & "E:\Fldr\scrpt.ps1" then I want scrpt.ps1 to get executed.

Is there a way to do this?

Thank you for checking in..


Solution

  • You can wrap your call to the script in a function:

    function Invoke-Script
    {
        E:\Fldr\scrpt.ps1
    }
    

    Then you can run your script by executing a single command anywhere after the definition:

    Invoke-Script
    

    Note that it is good practice to name your functions according to the Verb-Noun cmdlet naming standard, so something like Invoke-Script instead of exeScrpt. If you really want a single word as the name, then you can additionally create an alias for your function:

    New-Alias -Name exeScrpt -Value Invoke-Script