File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -18,23 +18,52 @@ Import-Module -Name {{ NAME }}
1818
1919## Usage
2020
21- Here is a list of example that are typical use cases for the module.
21+ The module provides two cmdlets that mirror PowerShell's built-in ` ConvertFrom-Json ` / ` ConvertTo-Json ` :
2222
23- ### Example 1: Greet an entity
23+ | Cmdlet | Alias | Purpose |
24+ | ------------------- | ---------------- | -------------------------------------- |
25+ | ` ConvertFrom-Yaml ` | ` ConvertFrom-Yml ` | Parse a YAML string into an object. |
26+ | ` ConvertTo-Yaml ` | ` ConvertTo-Yml ` | Serialize an object into a YAML string. |
2427
25- Provide examples for typical commands that a user would like to do with the module.
28+ ### Example 1: Parse a YAML string
2629
2730``` powershell
28- Greet-Entity -Name 'World'
29- Hello, World!
31+ $yaml = @'
32+ name: Alice
33+ age: 30
34+ skills:
35+ - PowerShell
36+ - YAML
37+ '@
38+
39+ $yaml | ConvertFrom-Yaml
3040```
3141
32- ### Example 2
42+ ### Example 2: Parse YAML as an ordered hashtable
3343
34- Provide examples for typical commands that a user would like to do with the module.
44+ ``` powershell
45+ Get-Content config.yaml -Raw | ConvertFrom-Yaml -AsHashtable
46+ ```
47+
48+ ### Example 3: Parse YAML frontmatter from a markdown file
49+
50+ ``` powershell
51+ Get-Content post.md -Raw | ConvertFrom-Yaml
52+ ```
53+
54+ ### Example 4: Convert an object to YAML
55+
56+ ``` powershell
57+ [ordered]@{
58+ name = 'Alice'
59+ skills = @('PowerShell', 'YAML')
60+ } | ConvertTo-Yaml
61+ ```
62+
63+ ### Example 5: Force a top-level YAML sequence
3564
3665``` powershell
37- Import-Module - Name PSModuleTemplate
66+ Get-Process | Select-Object -First 3 Name, Id | ConvertTo-Yaml -AsArray
3867```
3968
4069### Find more examples
Original file line number Diff line number Diff line change 11<#
2- . SYNOPSIS
3- This is a general example of how to use the module .
2+ . SYNOPSIS
3+ Example usage of the Yaml module: parse YAML to objects and convert objects back to YAML .
44#>
55
6- # Import the module
7- Import-Module - Name ' PSModule'
6+ Import-Module - Name ' Yaml'
87
9- # Define the path to the font file
10- $FontFilePath = ' C:\Fonts\CodeNewRoman\CodeNewRomanNerdFontPropo-Regular.tff'
8+ # 1. Parse a YAML string into a PSCustomObject
9+ $yaml = @'
10+ name: Alice
11+ age: 30
12+ skills:
13+ - PowerShell
14+ - YAML
15+ '@
1116
12- # Install the font
13- Install-Font - Path $FontFilePath - Verbose
17+ $person = $yaml | ConvertFrom-Yaml
18+ $person.name # Alice
19+ $person.skills [0 ] # PowerShell
1420
15- # List installed fonts
16- Get-Font - Name ' CodeNewRomanNerdFontPropo-Regular'
21+ # 2. Parse YAML as an ordered hashtable
22+ $hash = $yaml | ConvertFrom-Yaml - AsHashtable
23+ $hash [' age' ] # 30
1724
18- # Uninstall the font
19- Get-Font - Name ' CodeNewRomanNerdFontPropo-Regular' | Uninstall-Font - Verbose
25+ # 3. Parse YAML frontmatter from a markdown document
26+ $markdown = @'
27+ ---
28+ title: Hello world
29+ draft: false
30+ ---
31+ # Body
32+
33+ Markdown content here.
34+ '@
35+
36+ $frontmatter = $markdown | ConvertFrom-Yaml
37+ $frontmatter.title # Hello world
38+
39+ # 4. Convert an object to YAML
40+ [ordered ]@ {
41+ name = ' Alice'
42+ age = 30
43+ skills = @ (' PowerShell' , ' YAML' )
44+ } | ConvertTo-Yaml
45+
46+ # 5. Force a top-level sequence with -AsArray
47+ Get-Process | Select-Object - First 3 Name, Id | ConvertTo-Yaml - AsArray
48+
49+ # 6. Round-trip
50+ $obj = [ordered ]@ { a = 1 ; b = @ (' x' , ' y' ) }
51+ $obj | ConvertTo-Yaml | ConvertFrom-Yaml - AsHashtable
You can’t perform that action at this time.
0 commit comments