locked
Any way to run a powershell script in a directory with spaces in it, just by doubleclicking on it. RRS feed

  • Question

  • Hello,

    Is there anyway to run a powershell script in a  directory with spaces in it, just by doubleclicking on it?

    For example I browse to c:\my folder with explorer and click on myscript.ps1, it won't run.  It runs just fine if I open a powershell console.

    -Eric

    Thursday, December 30, 2010 9:30 PM

Answers

  • Hi,

     

    As a work around, you can write a batch file to achieve this. Open a notepad, input the following command:

     

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit “location of ps1 file”

    Save the file as double.bat.

    Or create a shortcut for this script naming it with the above line. Please visit the following similiar request:

    run ps1 script

    Best Regards

    Dale


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
    • Marked as answer by Shay Levi Friday, December 31, 2010 11:37 AM
    Friday, December 31, 2010 5:11 AM
  • Hello,

    I needed the -f switch. So now I'm able to run my batch file without hard-coding the path to the ps1 file. My batch file looks like: 

    set "dblquot=""
    powershell.exe -f %dblquot%%~dp0test-me.ps1%dblquot%

    -Eric

     

     

     

     

     

     

    Wednesday, January 5, 2011 10:07 PM

All replies

  • For security reasons, the default behavior when you double click on a *.ps1 file is to edit the file, not run the script. That could be dangerous. Others  may know how to modify this behavior.

    Richard Mueller


    MVP ADSI
    Thursday, December 30, 2010 10:51 PM
  • The default behavior on my machine is that it attempts to run it.  It fails if there are any spaces in the path.  I could have done something to change the behavior, but I don't think so.

    Eric

    Friday, December 31, 2010 3:38 AM
  • Hi,

     

    As a work around, you can write a batch file to achieve this. Open a notepad, input the following command:

     

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit “location of ps1 file”

    Save the file as double.bat.

    Or create a shortcut for this script naming it with the above line. Please visit the following similiar request:

    run ps1 script

    Best Regards

    Dale


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
    • Marked as answer by Shay Levi Friday, December 31, 2010 11:37 AM
    Friday, December 31, 2010 5:11 AM
  • Dale,

    That workaround won't really work for me.  The command that I'm scripting builds a bunch of visual studio projects, since I don't know what the other developers on my team's workspace mappings are going to be,  I can't hard code the path like this.

    I tried a workaround, using a bit more sophisticated batch script, the output looks correct in a command prompt, but it doesn't work.

    Here's what I did, create a new directory "c:\test me", or any other path you'd like, just make sure it has spaces in it.

    Here's the powershell script to run (named test-me.ps1):

     

    write-host "Hello there."
    

     

    I wrote a batch file that looks like, I put this in the  "c:\test me\" directory:

    set "dblquot=""
    set "sngquot='"
    powershell.exe %sngquot%%~dp0test-me.ps1%sngquot%

    So here's what I see as the contents of my "test me" directory:

     

     

    C:\test me>dir
     Volume in drive C has no label.
     Volume Serial Number is 123abc
    
     Directory of C:\test me
    
    01/03/2011 02:17 PM <DIR>   .
    01/03/2011 02:17 PM <DIR>   ..
    01/03/2011 02:17 PM    83 RunTestMe.bat
    01/03/2011 02:15 PM    25 test-me.ps1
        2 File(s)   108 bytes
        2 Dir(s) 31,591,268,352 bytes free
    

     

    If I cd to c:\test me in Powershell and run the test-me.ps1 script I see:

     

    PS C:\test me> .\test-me.ps1
    Hello there.

     

    If I open cmd.exe and go to that folder I see the following using the %sngquot% environment variable:

     

    C:\test me>runtestme.bat
    
    C:\test me>set "dblquot=""
    
    C:\test me>set "sngquot='"
    
    C:\test me>powershell.exe 'C:\test me\test-me.ps1'
    
    
    
    C:\test me\test-me.ps1

     

    There are no errors, but notice that you don't see a "Hello there."

    I change my batch file slightly to use double quotes (actually this was my original idea).

    So now my batch file looks like:

     

    set "dblquot=""
    set "sngquot='"
    powershell.exe %dblquot%%~dp0test-me.ps1%dblquot%

     

     I then run it with the following results:

     

    C:\test me>RunTestMe.bat
    
    C:\test me>set "dblquot=""
    
    C:\test me>set "sngquot='"
    
    C:\test me>powershell.exe "C:\test me\test-me.ps1"
    
    
    The term 'C:\test' is not recognized as the name of a cmdlet, function, script file, or operable pr
    ogram. Check the spelling of the name, or if a path was included, verify that the path is correct a
    nd try again.
    At line:1 char:8
    + C:\test <<<< me\test-me.ps1
     + CategoryInfo   : ObjectNotFound: (C:\test:String) [], CommandNotFoundException
     + FullyQualifiedErrorId : CommandNotFoundException
    
    
    C:\test me> 

     

    As you can see the command line it's trying to run is powershell.exe "C:\test me\test-me.ps1", but it's still getting confused by the spaces.  Any suggestions?

    -Eric

    Monday, January 3, 2011 7:35 PM
  • I tried hardcoding the path, but it still doesn't work.  Here's my batch file, named double.bat:

    dir "c:\test me\test-me.ps1"
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit "c:\test me\test-me.ps1"

    Here's what I see when I run my batch file:

    c:\test me>double
    
    c:\test me>dir "c:\test me\test-me.ps1"
     Volume in drive C has no label.
     Volume Serial Number is abc123
    
     Directory of c:\test me
    
    01/03/2011 02:15 PM        25 test-me.ps1
            1 File(s)       25 bytes
            0 Dir(s) 31,159,857,152 bytes free
    
    c:\test me>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit "c:
    \test me\test-me.ps1"
    
    
    
    The term 'c:\test' is not recognized as the name of a cmdlet, function, script
    file, or operable program. Check the spelling of the name, or if a path was inc
    luded, verify that the path is correct and try again.
    At line:1 char:8
    + c:\test <<<< me\test-me.ps1
      + CategoryInfo     : ObjectNotFound: (c:\test:String) [], CommandNotF
      oundException
      + FullyQualifiedErrorId : CommandNotFoundException
    
    PS C:\test me>

     I did the dir command first, so that you could see the path is valid, even though I'm putting double quotes around the path to my ps1 file, it still doesn't work.

    Thanks,

    Eric

    Monday, January 3, 2011 10:46 PM
  • It looks like there are some differences in the way powershell runs in different operating systems.  For the record, I'm doing my testing in Vista 32-bit.

    Thanks,

    Eric

    Tuesday, January 4, 2011 7:46 PM
  • Hello,

    I needed the -f switch. So now I'm able to run my batch file without hard-coding the path to the ps1 file. My batch file looks like: 

    set "dblquot=""
    powershell.exe -f %dblquot%%~dp0test-me.ps1%dblquot%

    -Eric

     

     

     

     

     

     

    Wednesday, January 5, 2011 10:07 PM
  • This can be accomplished via modifying a registry value, as I show in my blog post.

    Essentially you just need to change the key:

        HKEY_CLASSES_ROOT\Applications\powershell.exe\shell\open\command

    Change the "(Default)" value to:

        "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "& \"%1\""

    This just wraps the parameter being passed to powershell.exe (i.e. the script file path) in double quotes.


    - Dan - "Can't never could do anything"

    Tuesday, May 28, 2013 7:41 PM