split output fullname

Hi all,

I am trying to remove certain folders from the output fullname path.
In short, instead of H:/main/demo/first/ I would like something like this, /main/demo/first

Here is the code I am working with

 Function Get-Depth {
    Param(
        [String]$Path = 'H:\main\',
        [String]$Filter = "*",
        [Int]$ToDepth = 1,
        [Int]$CurrentDepth = 0
    )
    #incrimintation
    $CurrentDepth++

    Get-ChildItem $Path | %{
        $_ | ?{ $_.Name -Like $Filter }
        If ($_.PsIsContainer) {
            If ($CurrentDepth -le $ToDepth) {
 
                # Call to function
                #adds the filter values and depth to the path..
                Get-Depth -Path $_.FullName -Filter $Filter `
                  -ToDepth $ToDepth -CurrentDepth $CurrentDepth
            }
        }
    }
   
}
Get-Depth|? {$_.PsIsContainer}| select @{Name='Date Modified'; Expression={$_.LastWriteTime.ToString('MM/dd/yyyy')}}, @{Name='Owner'; E={(($_.GetAccessControl().Owner.Split('\'))[1])}}, @{Name='Structure'; E={(($_.FullName.Split('\'))[-1])}}

Thank you!

A couple of passes through split-path should do it - the drive and the filename will be stripped off just leaving the folder path

PS> Get-ChildItem  .\HyperV\ -Recurse  | foreach {Split-Path -Path $_.Fullname  -Parent | Split-Path  -NoQualifier  }
\Scripts\HyperV
\Scripts\HyperV
\Scripts\HyperV
\Scripts\HyperV
\Scripts\HyperV
\Scripts\HyperV
\Scripts\HyperV
\Scripts\HyperV
\Scripts\HyperV
\Scripts\HyperV
\Scripts\HyperV
\Scripts\HyperV\Setup
\Scripts\HyperV\Setup
\Scripts\HyperV\Setup
\Scripts\HyperV\Setup
\Scripts\HyperV\Setup
\Scripts\HyperV\Setup
\Scripts\HyperV\Setup
\Scripts\HyperV\Setup
\Scripts\HyperV\Setup
\Scripts\HyperV\Setup

BTW It is extremely bad practice to use aliases in your scripts ESPECIALLY ? and %

Hi Richard,

Thank you for the feedback. Can you suggest any alternatives for the aliases ?

Thanks

The recommendation for aliasses is: It’s fine for the console for “quick 'n dirty” things, but do not use them in scripts. Never. :wink:
You can get a list of all alieasses with this:

get-alias

@Olaf Soyak Thank you! I will make those changes immediately, btw, i was using them in scripts… sorry.