Skip to content

Instantly share code, notes, and snippets.

@davetapley
Last active February 26, 2022 00:15
Show Gist options
  • Save davetapley/16881224fbcd0fdc7cafb3beacef88f5 to your computer and use it in GitHub Desktop.
Save davetapley/16881224fbcd0fdc7cafb3beacef88f5 to your computer and use it in GitHub Desktop.
Powershell hashtable key clone weirdness
# 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