Skip to content

Add Export-PowerShellDataFile function for writing PSD1 files #11

Description

PowerShell ships Import-PowerShellDataFile but has no built-in Export-PowerShellDataFile. Modules that need to persist a hashtable to disk in PSD1 form — module manifests, configuration snapshots, generated localized data files — currently have to compose the file by hand string-by-string, or call New-ModuleManifest (which is manifest-specific and inflexible).

This issue adds the missing Export-PowerShellDataFile. There is intentionally no Import-PowerShellDataFile here — the built-in already covers that surface; the module re-exports the built-in (or omits it entirely).

Request

Desired capability

$config | Export-PowerShellDataFile -Path settings.psd1
$manifest | Export-PowerShellDataFile -Path module.psd1 -NoClobber

Standard parameters

Parameter Description
-Path File path (position 0, default parameter set)
-LiteralPath Literal path (no wildcard expansion)
-InputObject Hashtable to export (ValueFromPipeline, mandatory)
-Encoding Default utf8NoBOM
-NoClobber Fail if target exists
-Force Overwrite read-only files
-Indent Spaces per nesting level (default: 4)

All ConvertTo-PowerShellDataFile parameters pass through.

Acceptance criteria

  • $h | Export-PowerShellDataFile -Path f.psd1 produces a file readable by the built-in Import-PowerShellDataFile
  • Output round-trips: Import-PowerShellDataFile $f returns an equivalent hashtable
  • -NoClobber, -Force, -Encoding, -LiteralPath behave as in Export-Csv
  • ShouldProcess support (-WhatIf, -Confirm)
  • The generated file passes Test-ModuleManifest when the input represents a valid module manifest

Technical decisions

Architecture: Thin wrapper around ConvertTo-PowerShellDataFile (tracked separately). Adds only file I/O and the standard file-cmdlet parameters.

Function placement: src/functions/public/Export-PowerShellDataFile.ps1.

Parameter sets: Path (default, position 0) and LiteralPath, mirroring Export-Csv.

No Import-PowerShellDataFile in this module: The built-in already exists. Re-implementing or shadowing it adds risk without benefit. If the module ever needs to extend the built-in (e.g., string parsing), that lives under ConvertFrom-PowerShellDataFile rather than a competing Import- function.

Alias: Export-Psd1.

Encoding default: UTF-8 without BOM — matches PowerShell 7+ default for Set-Content.


Implementation plan

  • Add Export-PowerShellDataFile in src/functions/public/Export-PowerShellDataFile.ps1 with Export-Psd1 alias
  • Add tests/Export-PowerShellDataFile.Tests.ps1 — basic write, -NoClobber, -Force, -Encoding, -WhatIf, pipeline input, round-trip via built-in Import-PowerShellDataFile
  • Add module-manifest validation test (output passes Test-ModuleManifest)
  • Update examples/General.ps1 and README

Related

  • ConvertFrom/ConvertTo-PowerShellDataFile (prerequisite — separate issue)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions