I am working on to build an automation using Powershell to maintain the proper directory architecture for each individual file inside a server and track them with a record.

We have a parent directory which is D:\ParentDir and inside ParentDir, we do have multiple nested subdirectories. All here I am trying to get is, if I pick up a file inside these sub directories, what are the sub-directories behind the file from the Parent Directory.

let say

$dir = "D:\ParentDir" 
$file = "D:\ParentDir\subdir1\subdir2\subdir3\file1"

Here I want to write a PowerShell script to get the folder list between $dir and file1 like

$dir + n + file1 --> trying to get the folders list 'n'

In the above example, n is \subdir1\subdir2\subdir3

Is there any way to get that information ?

1

There are 1 best solutions below

1
On BEST ANSWER
Set-Location $dir
$file |Split-Path |Resolve-Path -Relative