如何在Powershell中获取父目录?

时间:2021-07-11 23:59:59

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?

我能在一行代码中找到rootPath吗?

9 个解决方案

#1


98  

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

如果$scriptPath指向一个文件,那么您必须首先调用文件上的目录属性,因此调用应该是这样的

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

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

注意,这只在$scriptPath存在时有效。否则必须使用分路cmdlet。

#2


19  

I've solved that like this:

我像这样解决了这个问题:

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

#3


15  

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.

你必须将反斜杠翻倍才能在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:

split-path看参数,这需要作为管道输入的路径,所以:

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

#4


5  

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


4  

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

在PowerShell 3中,$PsScriptRoot或者针对两个家长的问题,

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

#6


3  

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

你可以简单地将你需要的任何分割路径链起来:

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

#7


1  

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

#8


0  

If you want to use $PSScriptRoot you can do

如果您想使用$PSScriptRoot,可以这样做

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

#9


-1  

In powershell :

powershell:

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

this_script_path =美元(获取项目(MyInvocation.MyCommand.Path美元)美元).DirectoryName

$parent_folder = Split-Path $this_script_path -Leaf

$parent_folder =分路路径$this_script_path -Leaf

#1


98  

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

如果$scriptPath指向一个文件,那么您必须首先调用文件上的目录属性,因此调用应该是这样的

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

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

注意,这只在$scriptPath存在时有效。否则必须使用分路cmdlet。

#2


19  

I've solved that like this:

我像这样解决了这个问题:

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

#3


15  

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.

你必须将反斜杠翻倍才能在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:

split-path看参数,这需要作为管道输入的路径,所以:

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

#4


5  

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


4  

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

在PowerShell 3中,$PsScriptRoot或者针对两个家长的问题,

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

#6


3  

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

你可以简单地将你需要的任何分割路径链起来:

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

#7


1  

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

#8


0  

If you want to use $PSScriptRoot you can do

如果您想使用$PSScriptRoot,可以这样做

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

#9


-1  

In powershell :

powershell:

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

this_script_path =美元(获取项目(MyInvocation.MyCommand.Path美元)美元).DirectoryName

$parent_folder = Split-Path $this_script_path -Leaf

$parent_folder =分路路径$this_script_path -Leaf