1

So, my command works just fine when I run it directly in PowerShell, but throws errors when I try to use it in my batch script. What I'm trying to do, is add a directory to the system path, but from a batch file (I have reasons). I know how to do it, and been successful in doing so, just can't seem to fix this.

I read some information that multiple commands are separated by semicolons, which I believe is probably what the problem is here, but if it is, I don't know how to escape it in the command.

This is the command I'm trying to use in my batch file.

@echo off

powershell "[Environment]::SetEnvironmentVariable('path', "$([Environment]::GetEnvironmentVariable('path', 'machine'));C:\to\a\new\path",'Machine');"

These are the errors that come up when running it.

At line:1 char:106
+ ... , $([Environment]::GetEnvironmentVariable('path', 'machine'));C:\usr\ ...
+                                                                  ~
Missing ')' in method call.
At line:1 char:117
+ ... ment]::GetEnvironmentVariable('path', 'machine'));C:\usr\bin,'Machine ...
+                                                                 ~
Missing argument in parameter list.
At line:1 char:127
+ ... t]::GetEnvironmentVariable('path', 'machine'));C:\usr\bin,'Machine');
+                                                                        ~
Unexpected token ')' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall

I really believe the problem is just that first semicolon that I'm trying to use in a string.

4
  • For a start, you have two ( and three ) in your code, so brackets mismatch would be expected. Did you accidentally copy/paste one command into the middle of something else, because as comment above says, I'm not really sure what you're trying to do here. with the second command part. Commented Sep 2, 2019 at 23:16
  • Actually I figured it out... @Lee_Dailey Commented Sep 2, 2019 at 23:19
  • @Josh It was because of the quoting I did Commented Sep 2, 2019 at 23:19
  • @AlienDrew - yep, your quotes were ... bizarrely located. [grin] Commented Sep 2, 2019 at 23:45

1 Answer 1

2

I figured it out... I wasn't the semicolons, but the quoting.

I found how to fix it by reading Powershell in a batch file - How do you escape metacharacters? (in the "Quoting headaches" bit).

So now it works when the command is formatted like so:

powershell "[Environment]::SetEnvironmentVariable('path', \""$([Environment]::GetEnvironmentVariable('path', 'machine'));C:\to\a\new\path\"",'Machine')"
Sign up to request clarification or add additional context in comments.

2 Comments

I still have no idea what you're trying to do with the C:\to\a\new\path\"",'Machine') part. As you say, semi colon breaks up commands so that on it's own means nothing.It's not part of what's escaped and added to the string, at least as per what you posted.
Okay @Josh... Everything inside of the \"" areas, is supposed to be a string. This string $([Environment]::GetEnvironmentVariable('path', 'machine'));C:\to\a\new\path\ Get's the original system path, and appends to it with a new path. Let's represent that as $updatedPath ... The command would then look like (assuming that updatedPath is set to the above) powershell "[Environment]::SetEnvironmentVariable('path', \""$updatedPath\"",'Machine');"

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.