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.
$PSScriptRootis defined for scripts in files. You're not running a file-based script, so$PSScriptRootis empty. If you want the current directory, usepwdor[Environment]::CurrentDirectory(depending on whether you want PowerShell's opinion, or the operating system's opinion).Write-Host (Split-Path (pwd) -Parent)? Or set a variable first:$path = pwd. This is a basic case of using a cmdlet's output as input for another cmdlet.