如何在Powershell中获取Parent的父目录?
所以如果我有一个目录存储在一个variables,说:
$scriptPath = (Get-ScriptDirectory); 现在我想find两个父级别的目录。
我需要一个很好的方式来做:
 $parentPath = Split-Path -parent $scriptPath $rootPath = Split-Path -parent $parentPath 
我可以在一行代码中访问rootPath吗?
版本的目录
  get-item是你在这里友好的帮手。 
 (get-item $scriptPath ).parent.parent 
如果你只想要string
 (get-item $scriptPath ).parent.parent.FullName 
版本的文件
 如果$scriptPath指向一个文件,那么你必须首先调用Directory属性,所以调用看起来像这样 
 (get-item $scriptPath).Directory.Parent.Parent.FullName 
  备注 
 这只会在$scriptPath存在时才起作用。 否则,您必须使用Split-Path cmdlet。 
你可以将它拆分成反斜杠,然后用负数组索引的倒数第二个来得到祖父目录名称。
 ($scriptpath -split '\\')[-2] 
你必须加倍反斜杠才能在正则expression式中转义。
要获得整个path:
 ($path -split '\\')[0..(($path -split '\\').count -2)] -join '\' 
并且,查看拆分path的参数,它将path作为pipe道input,因此:
 $rootpath = $scriptpath | split-path -parent | split-path -parent 
我已经解决了这个问题:
 $RootPath = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent 
您可以使用
 (get-item $scriptPath).Directoryname 
获取stringpath,或者如果你想要的目录types使用:
 (get-item $scriptPath).Directory 
 在PowerShell 3中, $PsScriptRoot或者为你的两个父母提问, 
 $dir = ls "$PsScriptRoot\..\.." 
如果你想使用$ PSScriptRoot,你可以做
 Join-Path -Path $PSScriptRoot -ChildPath ..\.. -Resolve 
在PowerShell中:
$ this_script_path = $(Get-Item $($ MyInvocation.MyCommand.Path))。DirectoryName
$ parent_folder =分割path$ this_script_path –Leaf