5

Usually powershell script files end with .ps1, and modules end with .psm1.

Can one have powershell scripts/modules in files with no extension? Just "build" or "start" and so on? Will this cause an issue in any specific environment?

If yes, Can one have powershell scripts/modules in files with other "texty" extensions like .sh, .ps etc?


Is there an easier way to invoke powershell scripts instead of keying in .\script.ps1 arg1, arg2 , for example like : go arg1, arg2?

3 Answers 3

6

No, you can't. The error message for Import-Module is pretty clear:

Import-Module : The extension '.xyz' is not a valid module extension. The supported module extensions are '.dll',
'.ps1', '.psm1', '.psd1', '.cdxml' and '.xaml'. Correct the extension then try adding the file

And if you try to call a script with a non-standard extension, Windows will simply pop the "What program would you like to open this with?" dialog. If you choose to open with Powershell, a new Powershell process will be spawned, which will just do the same thing.

If you try to assign a new extension, like .xyz to always be opened with Powershell, you will end up with an never-ending series of Powershell processes being spawned, each attempting in vain to open the file with a new instance of Powershell. I just tried it :)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the prompt answer! "never-ending series of Powershell processes be spawned"... Could we have stumbled across a windows vulnerability ? I've also edited my question.
I'm not sure if it's a vulnerability so much as an amusing way to shoot yourself in the foot. There are many such opportunities :)
And regarding your followup, two approaches. IF you have the full path to a script in a variable (e.g. $myscript), you can invoke like this & $myscript arg1 arg2. Or, a better solution is to author your useful scripts and functions as modules, so you can import everything at once as a library. Then you can alias your favorite functions/cmdlets with short names which are easy to invoke, and you don't have to deal with scripts at all after import.
4

This is what the alias feature in Powershell is for. You can create your script, ending correctly in .ps1, and then create an alias so that you can invoke it as build, start or whatever.

See the documentation: get-help about_aliases

Comments

2

Super late answer, but I wanted to do this for all the scripts in my bin folder, and it took me a while to figure it out. I wound up with this:

Get-ChildItem -path <bin where your ps1 files live> -Filter *.ps1 |
Foreach-Object{
    $name = $_.Name
    $scptName = $_.Name -split ".ps1",0,"simplematch"
    $scptName = $scptName[0]
    new-alias $name $scptName
}

And I also wanted it for all the python scripts in my python bin, which was more complicated but still doable:

Get-ChildItem -path <pybin path> -Filter *.py |
Foreach-Object{
    $name = $_.Name
    $scptName =  $_.Name -split '.py',0,'simplematch'
    $scptName = $scptName[0]
    $eName = $scptName -split '-',0,'simplematch'
    $eName = $eName[1]
    invoke-expression('
    $val' + $eName + ' = $name
    function doRun' + $eName + '{
        python "<pybin path>/$val' + $eName + '" $args
    }
    $alias = "doRun$ename"
    set-alias $scptName $alias
    ')
}

Note that you are invoke-expressioning on an expression containing filenames on your system, so this is a security problem if other people can potentially write to that directory. Since this is my bin directory and I'm execing this stuff all the time anyways I've decided it doesn't matter, but it could so keep that in mind.

There's also certainly a more elegant way to have accomplished this, but I wasn't sure what it was.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.