PowerShell 2从路径获取文件夹?

时间:2016-07-13 16:55:49

标签: powershell-v2.0

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

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

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

返回folderC。

1 个答案:

答案 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