0

首先,我分配这个字符串变量:

PS> $newpath="C:\Program Files (x86)\Intel\iCLS Client"

这有效:

PS> [Environment]::SetEnvironmentVariable('Path', $newpath, 'Machine')

这不会:

PS> Start-Process Powershell -ArgumentList "-NoExit -noprofile -noninteractive -command [Environment]::SetEnvironmentVariable('Path', $newpath, 'Machine')" -Wait

第二个示例返回:

At line:1 char:46
+ [Environment]::SetEnvironmentVariable('Path', C:\Program Files (x86)\Intel\iCLS  ...
+                                              ~
Missing expression after ','.
At line:1 char:47
+ [Environment]::SetEnvironmentVariable('Path', C:\Program Files (x86)\Intel\iCLS  ...
+                                               ~~~~~~~~~~
Unexpected token 'C:\Program' in expression or statement.
At line:1 char:98
+ ... ient, 'Machine')
+                    ~
Unexpected token ')' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingExpressionAfterToken

我究竟做错了什么?我认为它与字符串、空格或引号有关....

谢谢

4

1 回答 1

0

你是对的。因为您的路径包含空格,所以必须用引号引起来。这应该有效:

Start-Process Powershell -ArgumentList "-NoExit -noprofile -noninteractive -command [Environment]::SetEnvironmentVariable('Path', '$newpath', 'Machine')" -Wait
于 2014-06-23T14:49:40.233 回答