This seemed to me an error and I and was on the point of raising it as a bug on the Powershell github repo:
[code lang="powershell"]PS> "\this".Split( [char]'\', [StringSplitOptions]::RemoveEmptyEntries).Length
# >> 2[/code]
Presumably it is because [StringSplitOptions]::RemoveEmptyEntries is coerced to a [char] and so the line is parsed as:
[code lang="powershell"]PS> "\this".Split( ([char]'\', [StringSplitOptions]::RemoveEmptyEntries) ).Length[/code]
Instead of as
[code lang="powershell"]PS> \this".Split( (,[char]'\'), [StringSplitOptions]::RemoveEmptyEntries).Length[/code]
If the first parameter is a string not a character then it works as expected:
[code lang="powershell"]PS> "\this".Split( '\', [StringSplitOptions]::RemoveEmptyEntries).Length
# >> 1[/code]
But the really unfortunate case is :
[code lang="powershell"]PS> "\this".Split( [System.IO.Path]::DirectorySeparatorChar, [StringSplitOptions]::RemoveEmptyEntries).Length
# >> 2[/code]
which results in
[code lang="powershell"]PS> "\this".Split( [System.IO.Path]::DirectorySeparatorChar, [StringSplitOptions]::RemoveEmptyEntries).[0]
# >> $null
# instead of
# >> "this"[/code]
It turns out that it's fixed in Powershell 6 Beta; or to be more precise, it doesn't happen in PowerShell 6. What changed is that the underlying .Net framework has added new overloads to String.Split():
string[] Split(char separator, System.StringSplitOptions options) string[] Split(char separator, int count, System.StringSplitOptions options) string[] Split(string separator, System.StringSplitOptions options) string[] Split(string separator, int count, System.StringSplitOptions options)
Whereas PowerShell 5 only has these overloads available:
string[] Split(Params char[] separator) string[] Split(char[] separator, int count) string[] Split(char[] separator, System.StringSplitOptions options) string[] Split(char[] separator, int count, System.StringSplitOptions options) string[] Split(string[] separator, System.StringSplitOptions options) string[] Split(string[] separator, int count, System.StringSplitOptions options)
And so the best-match overload that PowerShell 6 chooses is different to PowerShell 5's best match.