Skip to content

Instantly share code, notes, and snippets.

@rleap-m
Last active August 12, 2020 16:59
Show Gist options
  • Save rleap-m/143efa9850fe3054ba16b1f736c8f795 to your computer and use it in GitHub Desktop.
Save rleap-m/143efa9850fe3054ba16b1f736c8f795 to your computer and use it in GitHub Desktop.
.Split() Method behaving differently in PowerShell v5 and v7 - causing issue with Docker install.ps1
# Try running this in PowerShell 5 and then in PowerShell 7 - you'll see different behavior
# with the .Split() method
$PSVersionTable.PSVersion
$tab = [char]9
$hasTab = "[$tab]"
$hasTabAndSpace = "[$tab ]"
$hasTab
$hasTabAndSpace
$hasTab.Split(' ')
$hasTab.Split($tab)
$hasTab.Split("$tab ") # 5 splits here, 7 doesn't
$hasTabAndSpace.Split("$tab ") # 5 vs 7 different
$hasTabAndSpace.Split("$tab ").count
# Simple way to show difference
'[ ]'.Split(' ') # Split on single space
'[ ]'.Split(' ') # Split on two spaces
# Demo the PS 5 vs 7 difference with the docker version string output
$tab = [char]9
"This is a tab: [$tab] Okay?"
$tmp = docker -v|Out-String
$tmp
$tmp = $tmp.Trim().Split("$tab ",3)
$tmp[2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment