Author Posts

April 23, 2018 at 4:27 pm

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.

April 23, 2018 at 5:10 pm

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.

April 23, 2018 at 6:40 pm

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?

April 23, 2018 at 6:45 pm

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

April 23, 2018 at 6:47 pm

0330g000000rXr89 is intended to be a positional parameter.

April 23, 2018 at 6:56 pm

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."

April 23, 2018 at 6:57 pm

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.