PowerShell Split after Pipe

13,074

You are almost there. You need to put your code within the Foreach-Object cmdlet:

Get-ChildItem -recurse $folder|
    Where-Object {$_.PSIsContainer}|
    Select-Object -ExpandProperty FullName |
    ForEach-Object {            
        $_.Split("\")[3]        
    }
Share:
13,074
4ndy
Author by

4ndy

Updated on June 21, 2022

Comments

  • 4ndy
    4ndy almost 2 years

    I use the following line in my script to return all the paths of all folders at the $folder location.

    dir -recurse $folder|?{$_.PSIsContainer}|select -ExpandProperty FullName
    

    This works. But: I only need the fourth element of each path.

    I've tried adding |{$_.Split("\")}[3]}with the [3] in various places but I am getting an error with the split command, that Expressions are only allowed as the first element of a pipeline.

    I've tried putting brackets around various sections, and putting the whole expression into brackets and into a split but I can't seem to find a way to attach the split to any part of the pipe... Is there another way, perhaps?