#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 can either pipe a hashtable object to this command or you can specify a variable name for a pre- defined hash table. If you use this option, specify the variable name without the $. This 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 when you use a variable name unless you use -Passthru. If you pipe a hashtable to this command, the new hashtable will automatically be written to the pipeline. 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 Inputobject A piped in hashtable object .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 .EXAMPLE PS C:\> $newhash = Get-Service spooler | ConvertTo-HashTable | Rename-HashTable -Key Machinename -NewKey Computername This command uses my ConvertTo-Hashtable command to turn an object into a hashtable. .NOTES NAME : Rename-HashTable VERSION : 1.0 LAST UPDATED: 1/22/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 **************************************************************** * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * **************************************************************** .LINK http://jdhitsolutions.com/blog/2013/01/rename-hashtable-key-revised .LINK About_hash_tables .INPUTS hashtable string .OUTPUTS hashtable #> [cmdletbinding(SupportsShouldProcess=$True,DefaultParameterSetName="Pipeline")] Param( [parameter(Position=0,Mandatory=$True, HelpMessage="Enter the name of your hash table variable without the `$", ParameterSetName="Name")] [ValidateNotNullorEmpty()] [string]$Name, [parameter(Position=0,Mandatory=$True, ValueFromPipeline=$True,ParameterSetName="Pipeline")] [ValidateNotNullorEmpty()] [object]$InputObject, [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" ) Begin { Write-Verbose -Message "Starting $($MyInvocation.Mycommand)" Write-verbose "using parameter set $($PSCmdlet.ParameterSetName)" } Process { #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 if ($InputObject) { $name="tmpInputHash" Set-Variable -Name $name -Scope $scope -value $InputObject $Passthru=$True } Write-Verbose (get-variable -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." } #Process End { #clean up any temporary variables Get-Variable -Name tmpInputHash -Scope $scope -ErrorAction SilentlyContinue | Remove-Variable -Scope $scope Write-Verbose -Message "Ending $($MyInvocation.Mycommand)" } #end } #end Rename-Hashtable #optional alias Set-Alias -Name rht -Value Rename-Hashtable