I have a string where in i'm trying to capture the substring before the third occurence of a slash (\) character.
I have used IndexOf. Is there a neater way to capture this.
$mainstring = "C:\ClusterStorage\Volume1\CSV-VM\CSV-VM-VHD.vhdx" $indexof1 = $mainstring.IndexOf("\") $indexof2 = $mainstring.IndexOf("\",$indexof1+1) $indexof3 = $mainstring.IndexOf("\",$indexof2+1) $subsetstring = $mainstring.Substring(0, $indexof3) Write-Host $subsetstring