Skip to content

Import-Hashtable

SYNOPSIS

Imports a hashtable from a specified file.

SYNTAX

Import-Hashtable [-Path] <String> [-ProgressAction <ActionPreference>] [<CommonParameters>]

DESCRIPTION

This function reads a file and imports its contents as a hashtable. It supports .psd1, .ps1, and .json files. - .psd1 files are imported using Import-PowerShellDataFile. This process is safe and does not execute any code. - .ps1 scripts are executed, and their output must be a hashtable. If the script does not return a hashtable, an error is thrown. - .json files are read and converted to a hashtable using ConvertFrom-Json -AsHashtable. This process is safe and does not execute any code.

If the specified file does not exist or has an unsupported format, an error is thrown.

EXAMPLES

EXAMPLE 1

Import-Hashtable -Path 'C:\config.psd1'

Output:

Name       Value
----       -----
Setting1   Enabled
Setting2   42

Imports a hashtable from a .psd1 file.

EXAMPLE 2

Import-Hashtable -Path 'C:\script.ps1'

Output:

Name       Value
----       -----
Key1       Value1
Key2       Value2

Executes the script and imports the hashtable returned by the .ps1 file.

EXAMPLE 3

Import-Hashtable -Path 'C:\data.json'

Output:

Name       Value
----       -----
username   johndoe
roles      {Admin, User}

Reads a JSON file and converts its content into a hashtable.

PARAMETERS

-Path

Path to the file containing the hashtable.

Type: String
Parameter Sets: (All)
Aliases:

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

-ProgressAction

{{ Fill ProgressAction Description }}

Type: ActionPreference
Parameter Sets: (All)
Aliases: proga

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

OUTPUTS

hashtable

NOTES

A hashtable containing the data from the imported file. The hashtable structure depends on the contents of the imported file.

https://psmodule.io/Hashtable/Functions/Import-Hashtable