Import-PowerShellDataFile

Imports values from a .PSD1 file without invoking its contents.

Syntax

Import-PowerShellDataFile
      [-Path] <String[]>
      [<CommonParameters>]
Import-PowerShellDataFile
      [-LiteralPath] <String[]>
      [<CommonParameters>]

Description

The Import-PowerShellDataFile cmdlet safely imports key-value pairs from hashtables defined in a .PSD1 file. The values could be imported using Invoke-Expression on the contents of the file. However, Invoke-Expression runs any code contained in the file. This could produce unwanted results or execute unsafe code. Import-PowerShellDataFile imports the data without invoking the code.

Examples

Example 1: Retrieve values from PSD1

This example retrieves the key-value pairs stored in the hashtable kept inside the Configuration.psd1 file. Get-Content is used to show the contents of the Configuration.psd1 file.

Get-Content .\Configuration.psd1
$config = Import-PowerShellDataFile .\Configuration.psd1
$config.AllNodes

@{
    AllNodes = @(
        @{
            NodeName = 'DSC-01'
        }
        @{
            NodeName = 'DSC-02'
        }
    )
}

Name                           Value
----                           -----
NodeName                       DSC-01
NodeName                       DSC-02

Parameters

-LiteralPath

The path to the file being imported. All characters in the path are treated as literal values. Wildcard characters are not processed.

Type:String[]
Aliases:PSPath, LP
Position:0
Default value:None
Accept pipeline input:True
Accept wildcard characters:False
-Path

The path to the file being imported. Wildcards are allowed but only the first matching file is imported.

Type:String[]
Position:0
Default value:None
Accept pipeline input:True
Accept wildcard characters:True

Outputs

Hashtable