Skip to content

Add ConvertTo-Psd1Literal cmdlet #27886

Closed as duplicate
Closed as duplicate

Description

Summary of the new feature / enhancement

PowerShell has Import-PowerShellDataFile for reading .psd1 files, however there is no built-in way to serialize PowerShell objects into .psd1 literal syntax.

A lot of people end up writing their own implementations for this. I have my own implementation here:

https://github.com/nos1dot618/PSToolbox/blob/master/Public/ConvertTo-Psd1Literal.ps1

It would be useful to have a standard ConvertTo-Psd1Literal cmdlet in PowerShell that can convert hashtables, arrays, strings, and other supported values into valid .psd1 syntax.

For example:

$data | ConvertTo-Psd1Literal

This would give users a standard way to generate .psd1 files instead of having to maintain their own implementations.

Proposed technical implementation details (optional)

The implementation could follow the same general approach as existing community implementations and support the types that can be represented as PowerShell data file literals.

The output should be valid .psd1 syntax and ideally round trip correctly through Import-PowerShellDataFile.

The exact supported types and formatting can be decided during implementation.

Activity

  1. added
    Issue-Enhancement
    Needs-Triage
    on Aug 25, 2026
  2. rhubarb-geek-nz commented on Aug 26, 2026

    @rhubarb-geek-nz

    Lets go back to 2016... #2875

    Thirteen years it's been, and yet, here you stand as if it were only yesterday.

    said Voldemort while waiting to save his hash table.

    Now if you were looking for the reverse of Import-PowerShellDataFile then I expect the natural thing would be to look for Export-PowerShellDataFile rather than ConvertTo-Psd1Literal.

    That shows up two. The first is my own rhubarb-geek-nz.PowerShellDataFile. The second is Utilities. Now for the second one, despite the grandiose claim

    A PowerShell module with a collection of functions that should have been in PowerShell to start with.

    It is just a random bag of ad-hoc scripts that anyone could have written and have no coherence other than some developer might want a couple of them. Apart from the Export-PowerShellDataFile, it has scripts for Git (which is not provided by Windows), for removing items provided by Windows PowerShell (which is a great advert for why more things should not actually be included), and a file content displayer because .. reasons.

    I think the test of time has shown there isn't a real need, or if there is PSGallery is quenching that thirst. It is not a common tool, it is a poor proprietary format, JSON or XML would serve general purpose needs far better. The only reason I wrote mine was because of the unwanted commentary/noise generated by New-ModuleManifest.

    The output should be valid .psd1 syntax and ideally round trip correctly through Import-PowerShellDataFile.

    I would suggest that is the entire purpose rather than a nice to have.

    The exact supported types and formatting can be decided during implementation.

    I would expect that has already been defined in Data Files

    The data in the file must be contained in a hashtable. This format only supports constant values. You can't use code or PowerShell expressions.

    Rather than writing anything new, and if it is deemed desirable then I propose my own C# solution previously mentioned.

  3. kilasuit commented on Sep 1, 2026

    @kilasuit
    Collaborator

    I do see value in this however the question was asked previously as mentioned.

    I'm going to close this as a duplicate of #2875 & reopen that issue

  4. removed
    Needs-Triage
    on Sep 1, 2026
  5. kilasuit commented on Sep 9, 2026

    @kilasuit
    Collaborator

    As mentioned this was previously asked for in #2875 & in #2783
    And would be Export-PowerShellDataFile instead of ConvertTo-Psd1Literal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions