Skip to content

Commit a3c9207

Browse files
Document YAML file workflows
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent ed94c66 commit a3c9207

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ The module exports:
2929
| --- | --- |
3030
| `ConvertFrom-Yaml` | Parse one or more YAML documents into PowerShell values. |
3131
| `ConvertTo-Yaml` | Serialize supported PowerShell values as YAML 1.2-compatible text. |
32+
| `Export-Yaml` | Serialize values and atomically write one YAML file. |
33+
| `Import-Yaml` | Strictly decode and parse YAML files. |
3234
| `Test-Yaml` | Test YAML syntax, tags, duplicate keys, and configured resource limits. |
3335

3436
## Parse YAML
@@ -84,6 +86,24 @@ $mapping = @'
8486
'@ | ConvertFrom-Yaml -AsHashtable
8587
```
8688

89+
## Import YAML files
90+
91+
`Import-Yaml` reads complete files with strict Unicode decoding and delegates
92+
parsing to `ConvertFrom-Yaml`. `-Path` expands wildcards and accepts `FileInfo`
93+
pipeline input; `-LiteralPath` preserves wildcard characters in filenames.
94+
Resolved files are deduplicated and read in deterministic path order.
95+
96+
```powershell
97+
$configs = Import-Yaml -Path '.\config\*.yaml' -AsHashtable
98+
Get-ChildItem -Path '.\services' -Filter '*.yaml' | Import-Yaml
99+
Import-Yaml -LiteralPath '.\config[production].yaml'
100+
```
101+
102+
UTF-8 without a byte order mark is the default. UTF-8, UTF-16, and UTF-32 byte
103+
order marks are detected automatically and override `-Encoding`. Malformed
104+
bytes terminate with a path-specific error. `-NoEnumerate` and all parser
105+
resource limits have the same behavior as `ConvertFrom-Yaml`.
106+
87107
## Serialize PowerShell values
88108

89109
`ConvertTo-Yaml` supports `PSCustomObject` and explicit PSObject note-property
@@ -123,6 +143,25 @@ Repeated acyclic collection references are emitted with anchors and aliases.
123143
Cyclic graphs and unsupported runtime objects fail specifically; values are
124144
never silently truncated or converted with `ToString()`.
125145

146+
## Export YAML files
147+
148+
`Export-Yaml` aggregates pipeline records like `ConvertTo-Yaml`, serializes the
149+
complete value before changing the filesystem, and atomically publishes a
150+
same-directory temporary file. It writes UTF-8 without a byte order mark, LF
151+
line endings, and exactly one final newline by default.
152+
153+
```powershell
154+
$config | Export-Yaml -Path '.\config.yaml'
155+
'one', 'two' | Export-Yaml -Path '.\items.yaml' -Encoding utf16LE
156+
$config | Export-Yaml -Path '.\generated\config.yaml' -CreateDirectory -PassThru
157+
```
158+
159+
Use `-NewLine CRLF` or `-NoFinalNewline` to change presentation. `-NoClobber`
160+
prevents replacement, while `-Force` permits replacing a read-only destination
161+
and preserves its read-only state. The switches are mutually exclusive.
162+
`-WhatIf` creates no directory or temporary file. `-PassThru` is the only mode
163+
that emits the final `FileInfo`.
164+
126165
## Validate YAML
127166

128167
```powershell

examples/General.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ $outputYaml = [ordered]@{
4040
$outputYaml
4141
$outputYaml | ConvertFrom-Yaml
4242

43+
# Atomically export one file, then import it with strict decoding.
44+
$configPath = Join-Path $env:TEMP 'yaml-example.yaml'
45+
$config | Export-Yaml -Path $configPath -PassThru
46+
$importedConfig = Import-Yaml -LiteralPath $configPath
47+
$importedConfig
48+
Remove-Item -LiteralPath $configPath
49+
4350
# Test syntax, duplicate keys, tags, and resource limits without conversion.
4451
$isValid = $outputYaml | Test-Yaml
4552
$isValid

0 commit comments

Comments
 (0)