How to get the nearest subdirectory of a file as output in PowerShell?

You could use the Directory property of the object:

Get-ChildItem $directoryPath -Recurse -File | ForEach-Object {
    "$($_.FullName) - $($_.Directory.Name)\$($_.Name)"
}

just use the name of the Directory object and then the name of the file.


The easiest way in my eyes is to split the string at the backslashes:

$splitString = "E:\Dump\SubDump3\Sub_SubDump1\DumpText-1.txt" -split "\\"

(since the backslash is the escape character, it has to be escaped by a second backslash).

$splitString now is an array of all the parts between the backslashes. Now you can access the last and the second-last part of this array by $splitString[-1] (the filename) and $splitString[-2] (the name of the direct parent folder).