拆分路径 -路径$PSCommandPath父级



我正在使用PowerShellVer。5.1.14393.2248我的脚本位于路径中:C:AnwendungenPowershellAddOnModulesPSScriptVersionChecker

我想获取父路径,例如:C:AnwendungenPowershellAddOnModules。 当我运行时:

$destination = Split-Path -Path $PSCommandPath -Parent
$destination 

我仍然会得到C:AnwendungenPowershellAddOnModulesPSScriptVersionChecker而不是C:AnwendungenPowershellAddOnModules

我做错了什么?

运行脚本时,应检查$PSCommandPath的输出。它可能不指向C:AnwendungenPowershellAddOnModulesPSScriptVersionChecker,因为当您调用:

Split-Path C:AnwendungenPowershellAddOnModulesPSScriptVersionChecker

你会得到C:AnwendungenPowershellAddOnModules.

about_Automatic_Variables:

$PSCommandPath
Contains the full path and file name of the script that is being run. 
This variable is valid in all scripts.
…
$PSScriptRoot
Contains the directory from which a script is being run. 
In Windows PowerShell 2.0, this variable is valid only in script modules
(.psm1). Beginning in Windows PowerShell 3.0, it is valid in all scripts.

用途:

$destination = Split-Path -Path (Split-Path -Path $PSCommandPath -Parent) -Parent

或者,在 Windows PowerShell 3.0 及更高版本中:

$destination = Split-Path -Path $PSScriptRoot -Parent

最新更新