Skip to content

Add ConvertFrom-PowerShellDataFile and ConvertTo-PowerShellDataFile functions #12

@MariusStorhaug

Description

PowerShell ships Import-PowerShellDataFile for safely reading .psd1 files from disk, but it has no string-based equivalent. Scripts that receive PSD1 content from non-file sources (HTTP responses, Git blobs, embedded strings, multi-document streams) currently have to write the content to a temporary file before parsing — or fall back to Invoke-Expression, which executes arbitrary code.

There is also no built-in way to serialize a hashtable back into PSD1 syntax. The Hashtable module's Format-Hashtable is close, but it produces general PowerShell hashtable literals rather than canonical PSD1 (which restricts allowed value types to a documented subset).

This is the foundational issue for the module — Import-, Export-, Test-, and Format- all build on these two functions.

Request

Desired capability

$data = ConvertFrom-PowerShellDataFile $psd1Content
$data = $psd1Content | ConvertFrom-PowerShellDataFile

$psd1 = ConvertTo-PowerShellDataFile $hashtable
$psd1 = $hashtable | ConvertTo-PowerShellDataFile -Indent 4

ConvertFrom-PowerShellDataFile parameters

Parameter Description
-InputObject PSD1 string content (ValueFromPipeline, default set, position 0)
-SkipLimitCheck Bypass the 500-key / 5000-AST-node safety limits (matches the built-in Import-PowerShellDataFile)

Returns [hashtable] — same shape as the built-in Import-PowerShellDataFile.

ConvertTo-PowerShellDataFile parameters

Parameter Description
-InputObject Hashtable, ordered dictionary, or PSCustomObject (ValueFromPipeline, position 0)
-Depth Maximum nesting depth (default: 1024)
-Indent Spaces per nesting level (default: 4 — PSD1 convention)
-EnumsAsStrings Render enum values as their string names

Returns [string].

Acceptance criteria

  • ConvertFrom-PowerShellDataFile parses PSD1 content using the PowerShell AST (no Invoke-Expression, same safety as Import-PowerShellDataFile)
  • Returns equivalent output to Import-PowerShellDataFile for the same content
  • ConvertTo-PowerShellDataFile produces output that round-trips through ConvertFrom-PowerShellDataFile
  • Only PSD1-allowed types are emitted: hashtables, arrays, strings, numbers, booleans, null, [datetime], [guid], [char], [scriptblock] (per the documented subset)
  • Throws a clear error when serializing a type that PSD1 does not allow (e.g., [pscustomobject] instances should be converted to hashtable first or rejected with guidance)
  • Pipeline input works for both functions

Technical decisions

Parser approach: Use [System.Management.Automation.Language.Parser]::ParseInput() and walk the resulting AST. This matches how the built-in Import-PowerShellDataFile validates safety. No Invoke-Expression, no iex, no shelling out.

Allowed types: Mirror the documented PSD1 subset. Reject other types at serialization time with a clear error.

Function placement: src/functions/public/ConvertFrom-PowerShellDataFile.ps1 and src/functions/public/ConvertTo-PowerShellDataFile.ps1.

Aliases: ConvertFrom-Psd1 / ConvertTo-Psd1 for shorter common usage.

Output types: [hashtable] for ConvertFrom, [string] for ConvertTo.

Indent default: 4 spaces — matches the PowerShell module manifest convention generated by New-ModuleManifest.

Relationship to existing functions: Replaces the placeholder Set-PSModuleTest function in the repo. Cleanup is part of this issue.

Breaking changes: None — there are no real public functions today.


Implementation plan

Core functions

  • Add ConvertFrom-PowerShellDataFile in src/functions/public/ConvertFrom-PowerShellDataFile.ps1 with ConvertFrom-Psd1 alias
  • Add ConvertTo-PowerShellDataFile in src/functions/public/ConvertTo-PowerShellDataFile.ps1 with ConvertTo-Psd1 alias

Tests

  • Add tests/ConvertFrom-PowerShellDataFile.Tests.ps1 — basic hashtable, nested hashtable, arrays, all allowed scalar types, malformed input
  • Add tests/ConvertTo-PowerShellDataFile.Tests.ps1 — round-trip via ConvertFrom-PowerShellDataFile, type rejection, -Indent, -Depth, -EnumsAsStrings
  • Add cross-validation test: output of ConvertFrom-PowerShellDataFile matches Import-PowerShellDataFile for identical content written to a temp file

Cleanup

  • Remove placeholder Set-PSModuleTest and its test
  • Update examples/General.ps1 and README

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions