Skip to content

ConvertTo-Hashtable

SYNOPSIS

Convert an object into a hashtable.

SYNTAX

ConvertTo-Hashtable [-InputObject] <Object> [-NoEmpty] [-Exclude <String[]>] [-Alphabetical] [-Ordered]
 [<CommonParameters>]

DESCRIPTION

This command will take an object and create a hashtable based on its properties. You can have the hashtable exclude some properties as well as properties that have no value.

EXAMPLES

EXAMPLE 1

PS C:\> Get-Process -id $pid |
Select-Object name,id,handles,workingset |
ConvertTo-Hashtable

Name                           Value
----                           -----
WorkingSet                     418377728
Name                           powershell_ise
Id                             3456
Handles                        958

EXAMPLE 2

PS C:\> $hash = Get-Service spooler |
ConvertTo-Hashtable -Exclude CanStop,CanPauseAndContinue -NoEmpty
PS C:\> $hash

Name                           Value
----                           -----
ServiceType                    Win32OwnProcess, InteractiveProcess
ServiceName                    spooler
ServiceHandle                  SafeServiceHandle
DependentServices              {Fax}
ServicesDependedOn             {RPCSS, http}
Name                           spooler
Status                         Running
MachineName                    .
RequiredServices               {RPCSS, http}
DisplayName                    Print Spooler

This created a hashtable from the Spooler service object, skipping empty properties and excluding CanStop and CanPauseAndContinue.

EXAMPLE 3

PS C:\> Get-Service bits |
Select-Object Name,Displayname,Status,
@{Name="Computername";Expression={$_.Machinename}} |
ConvertTo-Hashtable -Alphabetical

Name                           Value
----                           -----
Computername                   .
DisplayName                    Background Intelligent Transfer Service
Name                           bits
Status                         Running

Convert an object to a hashtable and order the properties alphabetically.

PARAMETERS

-InputObject

A PowerShell object to convert to a hashtable.

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

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

-NoEmpty

Do not include object properties that have no value.

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

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

-Exclude

An array of property names to exclude from the hashtable.

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

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

-Alphabetical

Create a hashtable with property names arranged alphabetically.

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

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

-Ordered

Create an ordered hashtable instead of a plain hashtable.

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

Required: False
Position: Named
Default value: None
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

System.Object

OUTPUTS

System.Collections.Specialized.OrderedDictionary

NOTES

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

This was originally described at: http://jdhitsolutions.com/blog/2013/01/convert-powershell-object-to-hashtable-revised

About_Hash_Tables

Get-Member

Back to top