路径示例:'C:\ folderA \ folderB \ folderC \ script.ps1'
使用Split-Path
,我可以获得'folderC',但我如何获得'folderB'?
Split-Path (Split-Path $MyInvocation.MyCommand.Definition -Parent) -Leaf
返回folderC。
答案 0 :(得分:1)
你可以添加另一个Split-Path
Split-Path (Split-Path (Split-Path $MyInvocation.MyCommand.Definition -Parent) -Parent) -Leaf
作为管道,它看起来像这样
Split-Path $MyInvocation.MyCommand.Definition -Parent |
Split-Path -Parent |
Split-Path -Leaf