PowerShell 2 从路径获取文件夹?

PowerShell 2 Get Folder from Path?

路径示例:'C:\folderA\folderB\folderC\script.ps1'

使用Split-Path,我可以获得'folderC',但我如何获得'folderB'?

Split-Path (Split-Path $MyInvocation.MyCommand.Definition -Parent) -Leaf

这个returns文件夹C.

好吧,你可以再添加一个 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