Skip to content

Rename-Hashtable

SYNOPSIS

Rename a hashtable key.

SYNTAX

Pipeline (Default)

Rename-Hashtable [-InputObject] <Object> [-Key] <String> [-NewKey] <String> [-Passthru] [-Scope <String>]
 [-WhatIf] [-Confirm] [<CommonParameters>]

Name

Rename-Hashtable [-Name] <String> [-Key] <String> [-NewKey] <String> [-Passthru] [-Scope <String>] [-WhatIf]
 [-Confirm] [<CommonParameters>]

DESCRIPTION

This command will rename a key in an existing hashtable or ordered dictionary. You can either pipe a hashtable object to this command or you can specify a variable name for a pre-defined hashtable. 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 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.

EXAMPLES

EXAMPLE 1

PS C:\> Rename-Hashtable -name MyHash -key Name -newKey Computername

EXAMPLE 2

PS C:\> $newhash = Get-Service spooler |
ConvertTo-HashTable |
Rename-Hashtable -Key Machinename -NewKey Computername

This command uses the ConvertTo-Hashtable command from the PSScriptTools module to turn an object into a hashtable. The Machinename key is then renamed to Computername.

PARAMETERS

-Name

The variable name of your hash table. DO NOT include the $.

Type: System.String
Parameter Sets: Name
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-InputObject

A piped in hashtable object

Type: System.Object
Parameter Sets: Pipeline
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False

-Key

The name of the existing hashtable key you want to rename.

Type: System.String
Parameter Sets: (All)
Aliases:

Required: True
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-NewKey

The new name of the hashtable key.

Type: System.String
Parameter Sets: (All)
Aliases:

Required: True
Position: 3
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Passthru

Write the revised hashtable back to the pipeline. If you pipe a variable to this command, passthru will happen automatically.

Type: System.Management.Automation.SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-Scope

The scope where your variable is defined. The default is the global scope.

Type: System.String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: Global
Accept pipeline input: False
Accept wildcard characters: False

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.

Type: System.Management.Automation.SwitchParameter
Parameter Sets: (All)
Aliases: wi

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-Confirm

Prompts you for confirmation before running the cmdlet.

Type: System.Management.Automation.SwitchParameter
Parameter Sets: (All)
Aliases: cf

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

hashtable

OUTPUTS

None

Hashtable

NOTES

Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/

This code was first described at http://jdhitsolutions.com/blog/2013/01/Rename-Hashtable-key-revised

About_hash_tables

ConvertTo-Hashtable

Join-Hashtable

Back to top