PowerTip: Run a PowerShell Script with Space in the Path

Summary: Learn how to run a script with a space in the path.

Question: How do I run a script with a space in the path?

Answer 1: PS > c:my` foldermyscript.ps1

Answer 2: PS> &(“c:my  foldermyscript.ps1”)

Question: What is the easiest way to create an array?

Answer 1: $array = “1”,”2”,”3”,”4”

Answer 2: $array = 1..4

Comments (10)

  1. K_Schulte says:

    @Hieronymus: Me too!

    Short, precise and easy to remmeber!


  2. mredwilson says:

    @Hieronymus Thank you I hope you find them beneficial.

  3. mredwilson says:

    @Angel yes, the space disappeared, and that was the entire point of the tip 🙂 Thanks for the catch, and I added the space back in to the command.

  4. mredwilson says:

    @Klaus Thanks for your kind words.

  5. Hieronymus Fortesque Lickspittle says:

    I love the short PowerTips, thanks!

  6. angel says:

    I think you're missing a space in there:

    Answer 1: PS > c:my`foldermyscript.ps1

    produces an error on my machine. but:

    PS > c:my' foldermyscript.ps1

    works fine. Need to escape the space so PoSh doesn't interpret it as a separate argument or parameter. Unless I'm missing something to make the first one work?

  7. angel says:

    And then of course I make my own typo:

    PS > c:my` foldermyscript.ps1

    Apostrophe is not a grave.

  8. paul says:

    Also this works if you are executing a powershell script from a command interface and the name your script has a space in the path or file name.

    As in powershell -noexit .my` powershell` script.ps1

  9. derikogay says:

    You can use Long Path Tool for such problems, it works good.

  10. garfius1 says:

    The ONLY working solution for c# in System.Management.Automation

Skip to main content