How to get the Parent's parent directory in Powershell?

浪尽此生 提交于 2019-12-18 10:19:24

问题


So if I have a directory stored in a variable, say:

$scriptPath = (Get-ScriptDirectory);

Now I would like to find the directory two parent levels up.

I need a nice way of doing:

$parentPath = Split-Path -parent $scriptPath
$rootPath = Split-Path -parent $parentPath

Can I get to the rootPath in one line of code?


回答1:


Version for a directory

get-item is your friendly helping hand here.

(get-item $scriptPath ).parent.parent

If you Want the string only

(get-item $scriptPath ).parent.parent.FullName

Version for a file

If $scriptPath points to a file then you have to call Directory property on it first, so the call would look like this

(get-item $scriptPath).Directory.Parent.Parent.FullName

Remarks
This will only work if $scriptPath exists. Otherwise you have to use Split-Path cmdlet.




回答2:


I've solved that like this:

$RootPath = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent



回答3:


You can split it at the backslashes, and take the next-to-last one with negative array indexing to get just the grandparent directory name.

($scriptpath -split '\\')[-2]

You have to double the backslash to escape it in the regex.

To get the entire path:

($path -split '\\')[0..(($path -split '\\').count -2)] -join '\'

And, looking at the parameters for split-path, it takes the path as pipeline input, so:

$rootpath = $scriptpath | split-path -parent | split-path -parent



回答4:


You can use

(get-item $scriptPath).Directoryname

to get the string path or if you want the Directory type use:

(get-item $scriptPath).Directory



回答5:


In PowerShell 3, $PsScriptRoot or for your question of two parents up,

$dir = ls "$PsScriptRoot\..\.."



回答6:


You can simply chain as many split-path as you need:

$rootPath = $scriptPath | split-path | split-path



回答7:


Split-Path -Path (Get-Location).Path -Parent



回答8:


To extrapolate a bit on the other answers (in as Beginner-friendly a way as possible):

  • String objects that point to valid paths can be converted to DirectoryInfo/FileInfo objects via functions like Get-Item and Get-ChildItem.
  • .Parent can only be used on a DirectoryInfo object.
  • .Directory converts FileInfo object to a DirectoryInfo object, and will return null if used on any other type (even another DirectoryInfo object).
  • .DirectoryName converts a FileInfo object to a String object, and will return null if used on any other type (even another String object).
  • .FullName converts a DirectoryInfo/FileInfo object to a String object, and will return null if used on any other type (even another DirectoryInfo/FileInfo object).

Check the object type with the GetType Method to see what you're working with: $scriptPath.GetType()

Lastly, a quick tip that helps with making one-liners: Get-Item has the gi alias and Get-ChildItem has the gci alias.




回答9:


If you want to use $PSScriptRoot you can do

Join-Path -Path $PSScriptRoot -ChildPath ..\.. -Resolve



回答10:


In powershell :

$this_script_path = $(Get-Item $($MyInvocation.MyCommand.Path)).DirectoryName

$parent_folder = Split-Path $this_script_path -Leaf


来源:https://stackoverflow.com/questions/9725521/how-to-get-the-parents-parent-directory-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!