Last active
August 12, 2020 16:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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