1

I'm able to Write-Host a custom variable from PowerShell command line, but predefined variables are not working with same way.

What is proper way to echo a predefined variable.

Write-Host $path works.
Write-Host $PSScriptRoot does not.

Here is my code.

powershell.exe -ExecutionPolicy ByPass -Command "& { $path = """test"""; Write-Host $path; Write-Host $PSScriptRoot; }"

I would like to have parent directory of current script as variable.

Something like this $RootPath = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent.

4

1 回答 1

2

如果当前位置是您想要的,这一直对我有用。

(Get-Location).path

如果您还想了解其他预定义变量,Write host 应该可以工作。

Get-Variable或者只是Variable显示所有预定义的变量。如果你看$PSScriptroot,你会发现它是空的。这就是为什么它似乎对你不起作用。

于 2017-09-05T07:15:11.350 回答