Get great grand parent folder path (three levels up) in PowerShell?


Is there an elegant way to get the (great-grandparent folder) three levels up from a folder path?

I'm only looking to get C:\folderA\folderB from the full path, but both solutions just seem ugly to me.

$path = "C:\folderA\folderB\folderC\FolderD\folderE" # option 1 (Get-Item $path).parent.parent.parent.FullName # option 2 $path | Split-Path -Parent | Split-Path -Parent | Split-Path -Parent


Try this (works only if the path exists):

(Get-Item "$path\..\..\..").FullName

Alternatively, if the path does not exist:


You could also use this generic option for n levels:

[System.IO.Path]::GetFullPath($path + "\.." * $n)


I guess you could use a wrapper function... not exactly ideal but a little fun:

function Split-PathLots { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string] $Path , [int] $NumberOfTimes = 1 ) Process { $PathToReturn = $Path Write-Verbose "Input = $Path" [int]$i = 1 while ($i -le $NumberOfTimes) { $PathToReturn = $PathToReturn | Split-Path -Parent Write-Verbose "($i/$NumberOfTimes) $PathToReturn" $i++ } return $PathToReturn } } $path = "C:\folderA\folderB\folderC\FolderD\folderE" Write-Output (Split-PathLots -Path $path -NumberOfTimes 3 -Verbose)


VERBOSE: Input = C:\folderA\folderB\folderC\FolderD\folderE VERBOSE: (1/3) C:\folderA\folderB\folderC\FolderD VERBOSE: (2/3) C:\folderA\folderB\folderC VERBOSE: (3/3) C:\folderA\folderB C:\folderA\folderB


  • Directory path in C#
  • Create new absolute path from absolute path + relative or absolute path
  • The “GenerateJavaStubs” task failed
  • How to retrieve data from an XML file in a Sharepoint 2010 Feature Event Receiver?
  • Neo4j cypher query for X-chromosome ancestors
  • SQL how to do an outer join properly
  • How would i implement an infinite scroll in my DOM
  • css font-size and line-height not matching the baseline
  • Python: Split a String Field into 3 Separate Fields using Lambda
  • Graceful pod termination
  • insert a picture into database(sqlite) with java code. what should i do?
  • Getting error java.io.FileNotFoundException (log4j log file) at the time of publish project on cloud
  • how can I compare dates in array to find the earliest one?
  • Divide a $1 by 3 and adjusting 1 cent
  • How to Save JSON data to SQL server database in C#?
  • LESS CSS how to modify parent property in mixin
  • Angular2 - Template reference inside NgSwitch
  • How do I shift the decimal place in Python?
  • How to add a focus style to an editable ComboBox in WPF
  • Web.config system.webserver errors
  • Force show.bind execution
  • How do I access an unhandled exception in an MVC Error view?
  • Google Custom Search with transparent background
  • Disable Enter in editText android
  • Can Jackson SerializationFeature be overridden per field or class?
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • vba code to select only visible cells in specific column except heading
  • Symfony2: How to get request parameter
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How does Linux kernel interrupt the application?
  • Linking SubReports Without LinkChild/LinkMaster
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • To Get the radio button value in ruby on rails
  • How to push additional view controllers onto NavigationController but keep the TabBar?