Thomas
Thomas

Reputation: 69

Calling Powershell script from CMD with paths as parameters

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?

Upvotes: 0

Views: 322

Answers (2)

js2010
js2010

Reputation: 27418

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

Upvotes: 0

AdminOfThings
AdminOfThings

Reputation: 25001

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.

Upvotes: 1

Related Questions