PowerShell / Basic: editions, objects, types, and functions / Lesson 6
Separate providers, paths, bytes, and serialized data
Explain provider-specific operations, literal paths, content modes, encoding differences, and JSON/CSV contracts without touching host files.
On this page
Lesson goals
- Distinguish provider paths from arbitrary text.
- Choose line, raw-text, and byte contracts deliberately.
- Validate serialized schema and encoding independently of display.
A provider changes what a path operation means
PowerShell providers expose namespaces through common-looking commands. A filesystem item and a Windows Registry item do not have identical behavior, and a provider available on Windows may not exist on another platform. Current location and drive names influence path interpretation. Use the provider and cmdlet documentation rather than assuming every Get-Item or Set-Item operation refers to a file. An apparently simple path can therefore cross a host-changing or credential boundary. This course constructs data in memory only and never reads configured files, Registry state, services, or a provider drive.
Literal paths avoid wildcard expansion, not authorization
Path and LiteralPath parameters have different wildcard contracts where supported. Join-Path can express provider-aware composition more clearly than hardcoded separators, but it does not guarantee a permitted destination. Validate intended root, traversal, existence requirements, and overwrite policy independently. Test-Path observes one moment and cannot guarantee a later open or write succeeds. A private temporary path in a later authorized test still needs explicit ownership and cleanup. Do not use a real home, project, or service configuration path to make an example feel realistic. The actual course checks use no file effects.
Text lines, raw text, and bytes are different interfaces
Get-Content can expose line-oriented text or raw content under selected parameters, while byte-oriented operations have another contract. Newline translation, trailing delimiters, and decoding errors affect exact comparisons. PowerShell 7 commonly uses UTF-8 without a BOM for text output defaults, while Windows PowerShell 5.1 has cmdlet-specific legacy defaults including UTF-16LE in important output paths. Do not call every edition simply Unicode and assume identical files. Specify encoding when interoperability matters and verify bytes with a tool suited to the format. A correct console glyph does not prove the stored encoding or downstream parser behavior.
Serialization requires a schema and sufficient depth
ConvertTo-Json and ConvertFrom-Json transform data under documented type, depth, and option behavior. Explicitly choose depth for nested output rather than accepting an accidental truncated representation. CSV flattens tabular fields and needs rules for encoding, delimiters, quoting, nulls, and types. Neither format is a reason to feed arbitrary input into executable expressions. Keep object projection before display formatting and verify a round trip against the schema you actually promise. A round trip can change types or lose information if the format cannot represent every original property. Name that loss rather than treating parse success as equivalence.
$record = [pscustomobject]@{
Name = 'review'
Settings = @{ Workers = 2; Enabled = $true }
}
$record | ConvertTo-Json -Depth 4 -Compress
The intended JSON retains the nested settings. The PowerShell serializer was not executed, so no exact property-order or engine-output claim is made.
Validate the declared data and bytes without touching a file
An offline JSON and UTF-8 model can check synthetic content and exact round-trip properties. It does not establish PowerShellโs encoding defaults, ConvertTo-Json depth behavior, or CSV parameter semantics. Keep the data small and non-secret. A later authorized edition-specific test should inspect actual emitted bytes and schema using private temporary storage, including non-ASCII text, empty values, nested records, and malformed input. Separate parser evidence from application acceptance and preserve the original source when reviewing a conversion. No filesystem provider or host encoding setting is read or changed here.
import json
record = {'Name': 'review', 'Settings': {'Workers': 2, 'Enabled': True}}
payload = json.dumps(record, ensure_ascii=False, sort_keys=True).encode('utf-8')
restored = json.loads(payload.decode('utf-8'))
print('roundtrip=' + str(restored == record))
print('workers=' + str(restored['Settings']['Workers']))
roundtrip=True
workers=2
Only memory values are encoded and parsed. This verifies the Python fixture, not PowerShell defaults, file content, or a platform provider.
Common errors and fixes
LiteralPath is called path authorization
- Why it happens
- Wildcard handling was confused with destination policy.
- How to fix it
- Validate allowed destinations and effects separately.
- Prevent it next time
- Document provider and overwrite scope.
A 5.1 output file is assumed byte-identical to 7
- Why it happens
- Edition-specific encoding defaults were ignored.
- How to fix it
- Specify and test the required encoding.
- Prevent it next time
- Include non-ASCII round-trip cases.
Nested JSON silently loses the intended structure
- Why it happens
- Depth and schema were not part of the output contract.
- How to fix it
- Choose supported options and verify the decoded schema.
- Prevent it next time
- Keep serialization tests separate from display tests.
Check your understanding
1. Why is a successful JSON parse not proof of a lossless round trip?
Show worked solution
The format or selected serializer options may omit properties or change types and depth. Compare the promised schema and values after decoding, and state any intentional conversion rather than treating syntactic validity as complete equivalence.
2. What should a cross-edition text-output test inspect?
Show worked solution
Inspect actual bytes, BOM policy, newline behavior, encoding, and the consuming parser under each supported edition. A visually correct console string cannot establish those file properties. Use private synthetic fixtures rather than configured host files.
Key takeaways
- Separate provider, path policy, text mode, and byte encoding.
- Test serialized schema and exact representation at the correct boundary.