0

I`m having trouble make this cmd command to run powershell script by passing two parameters (paths).

C:\WINDOWS\System32>powershell -command \"C:\Apps\Scripts\Test\testing.ps1" \"\\Data1\dataholder$\office\J Smith\backup\" \"\\Data1\dataholder$\office\J Smith\backup2\"

I`m getting the error below:

At line:1 char:36
+ ... ps\Scripts\Test\testing.ps1 "\\Data1\dataholder$\office\ ...
+                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token '\\Data1\dataholder$\office' in expression or statement.
At line:1 char:146
+ ... Smith\backup" "\\Data1\dataholder$\office\J Smith\backup2"
+                                                                         ~
The string is missing the terminator: ".
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

Any idea of why this is failing?

I had to modify the path leading to script. Now the command look like that:

C:\WINDOWS\System32>powershell -command \"C:\Apps\Scripts\Power test\testing.ps1" \"\\Data1\dataholder$\office\J Smith\backup\" \"\\Data1\dataholder$\office\J Smith\backup2\"

This is failing due to the space in the script path, any idea how to handle this?

1
  • Your first quote escape is unnecessary \"C:\Apps\Scripts\Test\testing.ps1" --> "C:\Apps\Scripts\Test\testing.ps1" Commented Jun 18, 2020 at 14:39

2 Answers 2

1

You need to backslash escape quotes that you want to be sent from cmd shell to PowerShell:

powershell -command "& \"C:\Apps\Scripts\Power test\testing.ps1\" \"\\Data1\dataholder$\office\J Smith\backup\" \"\\Data1\dataholder$\office\J Smith\backup2\""

If your program or script file has spaces, you need to call it with a call operator (&) and include that in your string that is sent to -Command. Notice the double quotes around the entire value/string that maps to -Command.

Note: Inner double quotes work here because $ is succeeded by \. If you had dataholder$folder as an example, you would need single quotes or to escape the $ because PowerShell would try to interpret $folder as a variable.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, how to handle the space in the path leading to script? C:\Apps\Scripts\Power test\testing.ps1
I edited it to show how to do that. You need to use the call operator &, surround the entire command in quotes, then escape inner double quotes.
0

Here's a backquoting the spaces approach.

type "my script.ps1"

echo $args[0]


powershell .\my` script.ps1 hi` there

hi there

Or strategic placement of single quotes. Something can be executed if the first character isn't a quote. The quote didn't work before the backslash.

powershell .\'my script.ps1' 'hi there'

hi there

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.