#requires -version 2.0 Function Rename-HashTable { <# .SYNOPSIS Rename a hashtable key. .DESCRIPTION This command will rename a key in an existing hash table. You must specify the variable name that holds your hashtable, without the $. The command will create a temporary copy of the hashtable, create the new key and copy the value from the old key, before removing the old key. The temporary hashtable is then set as the new value for your original variable. This command supports -Whatif. This command does not write anything to the pipeline unless you use -Passthru. You might find this command useful when building a hashtable that you intend to use with splatting where you need to align key names with parameter names. .PARAMETER Name The variable name of your hash table. DO NOT use the $. .PARAMETER Key The name of the existing hash table you want to rename. .PARAMETER NewKey The new name of the hashtable key. .PARAMETER Passthru Write the revised hashtable back to the pipeline .PARAMETER Scope The scope where your variable is defined. The default is the global scope. .EXAMPLE PS C:\> Rename-Hashtable -name MyHash -key Name -newKey Computername .NOTES NAME : Rename-HashTable VERSION : 0.9 LAST UPDATED: 1/16/2013 AUTHOR : Jeffery Hicks (@JeffHicks) Read PowerShell: Learn Windows PowerShell 3 in a Month of Lunches Learn PowerShell Toolmaking in a Month of Lunches PowerShell in Depth: An Administrator's Guide .LINK http://jdhitsolutions.com/blog/2013/01/rename-hashtable-key .LINK About_hash_tables .INPUTS None .OUTPUTS None #> [cmdletbinding(SupportsShouldProcess=$True)] Param( [parameter(position=0,Mandatory=$True, HelpMessage="Enter the name of your hash table variable without the `$")] [ValidateNotNullorEmpty()] [string]$Name, [parameter(position=1,Mandatory=$True, HelpMessage="Enter the existing key name you want to rename")] [ValidateNotNullorEmpty()] [string]$Key, [parameter(position=2,Mandatory=$True, HelpMessage="Enter the NEW key name")] [ValidateNotNullorEmpty()] [string]$NewKey, [switch]$Passthru, [ValidateSet("Global","Local","Script","Private",0,1,2,3)] [ValidateNotNullOrEmpty()] [string]$Scope="Global" ) #validate Key and NewKey are not the same if ($key -eq $NewKey) { Write-Warning "The values you specified for -Key and -NewKey appear to be the same. Names are NOT case-sensitive" Return } Try { #validate variable is a hash table write-verbose (gv -Scope $scope | out-string) Write-Verbose "Validating $name as a hashtable in $Scope scope." #get the variable $var = Get-Variable -Name $name -Scope $Scope -ErrorAction Stop if ( $var.Value -is [hashtable]) { #create a temporary copy Write-Verbose "Cloning a temporary hashtable" <# Use the clone method to create a separate copy. If you just assign the value to $temphash, the two hash tables are linked in memory so changes to $tempHash are also applied to the original object. #> $tempHash = $var.Value.Clone() #validate key exists Write-Verbose "Validating key $key" if ($tempHash.Contains($key)) { #create a key with the new name using the value from the old key Write-Verbose "Adding new key $newKey to the temporary hashtable" $tempHash.Add($NewKey,$tempHash.$Key) #remove the old key Write-Verbose "Removing $key" $tempHash.Remove($Key) #write the new value to the variable Write-Verbose "Writing the new hashtable" Write-Verbose ($tempHash | out-string) Set-Variable -Name $Name -Value $tempHash -Scope $Scope -Force -PassThru:$Passthru | Select-Object -ExpandProperty Value } else { Write-Warning "Can't find a key called $Key in `$$Name" } } else { Write-Warning "The variable $name does not appear to be a hash table." } } #Try Catch { Write-Warning "Failed to find a variable with a name of $Name." } Write-Verbose "Rename complete" } #end Rename-Hashtable #optional alias #Set-Alias -Name rht -Value Rename-Hashtable