Executable extensions - Powershell-specific filetype associations RRS feed

  • Question

  • Is it possible to customize the behaviour of Powershell when asked to execute a file with a given extension? I'm not talking here about system-wide file associations - I know about these - but rather about having a different behaviour when the file is run from the Powershell prompt.

    As a simple example, I would normally want .vbs scripts to run using WScript when clicked on, so that's how I have my system-wide file association set. But when I run the script within Powershell, I'd rather use CScript.

    This is something that JPSoft's Take Command can do, and in that I have an association .vbs=cscript /nologo to give precisely this behaviour.

    Another example where I might want something like this (and this is actually the case that prompted me to ask the question) is if I want to set up in my profile, a Powershell function to run the file - in this case, the association makes no sense outside of Powershell, and so a Powershell-only approach is what I want (I was looking to emulate the Unix #! functionality, writing a function that parses the first line of the script and passes the script on to the specified interpreter).

    I guess that for the second case (and by extension, the first) I could associate my extension with Powershell plus appropriate arguments to run the function I want, but that incurs the cost of an extra Powershell process, and would be pretty complex to get right. It also results in a Powershell window in the VBScript case, which is precisely what I want to avoid by associating scripts with WScript in the first place. I've also tried searching the web, but it's hard to know what search terms would work :-(



    Monday, March 14, 2011 11:09 AM


  • No, associations are done system-wide, and trying to do something like dynamically change them will also be system-wide, and may actually require a reboot to enable and/or disable.
    • Marked as answer by Marco Shaw Saturday, March 19, 2011 1:29 AM
    Saturday, March 19, 2011 1:29 AM