When you turn a path into an array to manipulate parts of the path, if you access path elements by fixed index numbers, then this approach will only work if the path has a fixed number of subfolders.
To work with any path length, try using variables. This example will take out the first and second subfolder, regardless of path length:
$path = 'C:\users\Tobias\Desktop\functions.ps1' $array = $path -split '\\' $length = $array.Count $newpath = $array[,0+3..$length] $newpath -join '\'
Note how the array elements for the new path are picked:
$newpath = $array[,0+3..$length]
This line takes the first path element (index 0) and the elements 4 and all following elements (index 3 and more).
The secret here is that PowerShell allows you to submit arrays of indices. The expression x..y creates a numeric array of the range x to y where x and/or y can be also variables.
When you want to add individual indices, you have to turn them also into arrays because only arrays can be added to arrays. That's why the index 0 is written like this: ,0. This creates an array that only holds the zero, and this array can then be added to the numeric range array, resulting in one single array with all the indices you want.