Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Split after Pipe

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?

like image 752
4ndy Avatar asked May 02 '17 06:05

4ndy


1 Answers

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]        
    }
like image 72
Martin Brandl Avatar answered Oct 16 '22 22:10

Martin Brandl