powershell-yaml Documentation

repository·master·Indexed 19 days ago

https://github.com/cloudbase/powershell-yaml

A thin PowerShell wrapper around the YamlDotNet library for serializing and deserializing PowerShell objects to and from YAML format. Includes documentation for ConvertTo-Yaml and ConvertFrom-Yaml, support for multiple documents, YAML merge keys, and output formatting options.

Tokens
1.1K
Snippets
8
Records
8
Agent score
18%

What's inside powershell-yaml

  1. Use YAML tags to prevent type ambiguity

    master

    To ensure values are converted to the correct type (e.g., preventing a string like +40123 from being cast to an Int64), use explicit YAML tags. This module supports core schema tags and the !!timestamp tag.

    $data = @"
    aPhoneNumber: !!str +40123456789
    aPhoneNrWithoutTags: +40123456789
    "@
    $obj = ConvertFrom-Yaml $data
    # $obj.aPhoneNumber is a String
    # $obj.aPhoneNrWithoutTags is an Int64
  2. Parse multiple YAML documents with -AllDocuments

    master

    When a YAML string contains multiple documents separated by ---, use the -AllDocuments switch with ConvertFrom-Yaml. This returns an Object[] (array) where each element is the content of a document.

    Note: The resulting array may not translate back to the exact same multi-document YAML structure if passed back through ConvertTo-Yaml.

    $yaml = @"
    ---
    doc1: value
    ---
    doc2: value
    "@
    $obj = ConvertFrom-Yaml $yaml -AllDocuments
    # $obj[0] contains the first document
    # $obj[1] contains the second document
  3. Convert YAML to PowerShell objects with ConvertFrom-Yaml

    master

    Use ConvertFrom-Yaml to deserialize YAML strings into PowerShell objects. For a single YAML document, the result is typically a Hashtable.

    Import-Module powershell-yaml
    $yaml = @"
    anArray:
    - 1
    - 2
    - 3
    "
    $obj = ConvertFrom-Yaml $yaml
  4. Enable YAML merge keys support

    master

    To support YAML merge keys (using <<: *anchor), use the -UseMergingParser switch with ConvertFrom-Yaml.

    Warning: Attempting to overwrite a key that already exists via a merge key will throw a duplicate key exception.

    $mergingYaml = @"
    ---
    default: &default
      value1: 1
      value2: 2
    hoge:
      <<: *default
      value3: 3
    "@
    
    ConvertFrom-Yaml -Yaml $mergingYaml -UseMergingParser
  5. Convert PowerShell objects to YAML with ConvertTo-Yaml

    master

    Use ConvertTo-Yaml to serialize PowerShell objects (like Hashtables or Arrays) into YAML strings. By default, it uses Block style formatting.

    Import-Module powershell-yaml
    $yaml = ConvertTo-Yaml @{"hello"="world"; "anArray"=@(1,2,3); "nested"=@{"array"=@("this", "is", "an", "array")}}
  6. Convert YAML to JSON-compatible format

    master

    You can generate a JSON-compatible string from a PowerShell object by using the -JsonCompatible switch with ConvertTo-Yaml. This produces a single-line string without indentation, suitable for JSON-like structures.

    Import-Module powershell-yaml
    $obj = ConvertFrom-Yaml $yaml
    ConvertTo-Yaml -JsonCompatible $obj
    # Or in one line:
    ConvertFrom-Yaml $yaml | ConvertTo-Yaml -JsonCompatible
  7. Control YAML output formatting with -Options

    master

    You can change the visual style of the generated YAML using the -Options parameter in ConvertTo-Yaml:

    • UseFlowStyle: Outputs everything in Flow style (JSON-like braces/brackets).
    • UseSequenceFlowStyle: Outputs sequences in Flow style but keeps other elements in Block style.
    • WithIndentedSequences: Indents sequences within Block style (by default, sequences are not indented).

    Note: These options are additive/combinable where applicable.

    # Flow style
    ConvertTo-Yaml $data -Options UseFlowStyle
    
    # Sequence flow style
    ConvertTo-Yaml $data -Options UseSequenceFlowStyle
    
    # Indented sequences in block style
    ConvertTo-Yaml $data -Options WithIndentedSequences