newbie: Starting a script with spaces in pathname

Welcome Forums General PowerShell Q&A newbie: Starting a script with spaces in pathname

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
6 months, 4 weeks ago.

  • Author
    Posts
  • #99519

    Participant
    Points: 0
    Rank: Member

    I'm trying to start a script (with one positional argument) from the command line thusly:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "& {C:\Users\John\Documents\My Programs\myScript.ps1 330g000000rXr89}"

    The attempt fails due to the space in the pathname for the script. Various attempts at single-, double-quotes, doubled double-quotes, and using the DOS ^ escape character, have failed.

    Thanks in advance.

  • #99525

    Participant
    Points: 22
    Rank: Member

    When calling a PowerShell script from the command line, this is how I do it:

    powershell.exe -file "c:\Temp\Test Path\test.ps1"

    You're trying to tell PowerShell to launch a file, not a command. Just tested and it works with the space in the file path.

  • #99526

    Participant
    Points: 0
    Rank: Member

    Thanks, but that's how I started this mis-adventure. When I attempt the following:

     C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file "C:\Users\John\Documents\My Programs\myScript.ps1 0330g000000rXr89"

    I get the error:

    Processing -File 'C:\Users\John\Documents\My Programs\myScript.ps1 0330g000000rXr89' failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again.

    which clearly isn't correct. My original question arose working around this problem. BTW, the original works when there is not a space in the file pathname.

    I'm using PowerShell 3.0.

    Ideas?

    • #99537

      Participant
      Points: 0
      Rank: Member

      I thought I had tried this, but it must of slipped through the cracks. This works:

      C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file "C:\Users\John\Documents\My Programs\myScript.ps1" "0330g000000rXr89"

      Thanks to all that responded.

  • #99529

    Participant
    Points: 4
    Rank: Member

    Your file name does not end with .ps1. Try removing 0330g000000rXr89 from the end of the filename and see if that works.

    • #99532

      Participant
      Points: 0
      Rank: Member

      0330g000000rXr89 is intended to be a positional parameter.

  • #99534

    Participant
    Points: 4
    Rank: Member

    I am not sure about the positional parameter but the error you posted starts with "failed because the file does not have a '.ps1' extension."

The topic ‘newbie: Starting a script with spaces in pathname’ is closed to new replies.