Last active
February 26, 2022 00:15
-
-
Save davetapley/16881224fbcd0fdc7cafb3beacef88f5 to your computer and use it in GitHub Desktop.
Powershell hashtable key clone weirdness
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
# Use of script block for Group-Object -AsHashTable -Property breaks Keys.Clone() | |
# https://github.com/MicrosoftDocs/PowerShell-Docs/issues/8605 | |
Write-Output "Using -Property Name" | |
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/group-object?view=powershell-7.2#example-1--group-files-by-extension | |
$files = Get-ChildItem -Path $PSHOME | Group-Object -Property Name -AsHashTable | |
Write-Output $files.Keys.count | |
# https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-hashtable?view=powershell-7.2#iterating-hashtables | |
$files.Keys.Clone() | ForEach-Object { $files[$_] = 'test' } | |
Write-Output $files.Keys.count | |
Write-Output "Using -Property { `$_.Name } " | |
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/group-object?view=powershell-7.2#example-1--group-files-by-extension | |
$files = Get-ChildItem -Path $PSHOME | Group-Object -Property { $_.Name } -AsHashTable | |
Write-Output $files.Keys.count | |
# https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-hashtable?view=powershell-7.2#iterating-hashtables | |
$files.Keys.Clone() | ForEach-Object { $files[$_] = 'test' } | |
Write-Output $files.Keys.count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment