# Reliable Automation Patterns

Structure document jobs for repeatability, diagnostics, safe mutation, and CI artifacts. Includes examples and cmdlet links.

Document automation becomes easier to operate when scripts separate source data, document composition, validation, and delivery.

## Start with the smallest public surface

Use a pipeline for a direct export, a live document object for loop-driven composition, the DSL for a complete authored artifact, and targeted commands for an existing file. Escalating only when the job needs more structure keeps common scripts short without hiding advanced capabilities. See [pipeline, object, and DSL workflows](/docs/pswriteoffice/object-workflows/).

## Keep paths and data explicit

Resolve input and output roots once. Pass plain objects into document builders instead of reading global state from nested DSL blocks. Write validation output beside the artifact or into a CI report folder.

## Prefer canonical commands in reusable scripts

PSWriteOffice publishes aliases for a compact DSL, but canonical commands make long-lived scripts easier to search, document, and compare with generated help. Use aliases when they materially improve a local composition block; keep entry points and operational steps canonical.

## Inspect before mutation

For existing files, collect preflight, capability, signature, compliance, accessibility, or format-detection evidence before changing the document. Write to a new path during development and reopen the result through the same engine.

## Inspect and extract archives

`Get-OfficeArchive` lists members in archive order. Add `-Verify` to read selected payloads in full and run the available integrity checks. Inspect `IntegrityChecks` on each result to see which checks passed. A plain TAR can report `Verified` with only `Structure` checks. Use the inventory's zero-based `Ordinal` values to select specific members, including members that share an authored name.

```powershell
Get-Item -LiteralPath ./documents.zip | Get-OfficeArchive
Get-OfficeArchive -LiteralPath ./documents.zip -Verify -Ordinal 0
Expand-OfficeArchive -LiteralPath ./documents.zip -Destination ./unpacked -Entry 'reports/monthly.csv'
```

`Expand-OfficeArchive` publishes verified content to an absent or empty destination directory. Pass exact, case-sensitive relative file paths to `-Entry`, using forward slashes even on Windows. Names with no match are skipped. The extractor validates every discovered member path before applying the selection. Omit `-Entry` to select all members. Extraction fails if a selected member has an unsupported type. To skip TAR symbolic links with valid member names, list the regular files explicitly. Use `-WhatIf` to preview publication or `-Confirm` to request confirmation before extraction.

## Treat conversion as a result

Keep the source, destination, diagnostics, warnings, and any fidelity policy together. A successful file write does not by itself prove that complex layout or unsupported features were preserved.

## Make jobs idempotent

Create a fresh output folder or use stable file names with deliberate overwrite behavior. Avoid appending the same section, sheet, or slide on every rerun unless the script first detects whether it already exists.

For object workflows, leave out the constructor's content block, apply the complete change set to the live document, then use the matching `Save-*` and `Close-*` commands once. Saved DSL constructors are quiet by default; use `-PassThru` only when another command needs the saved file.

## Bound batch work

For Reader and conversion batches, limit concurrency based on document size and native-memory pressure. Capture failures per file so one malformed input does not erase evidence for the rest of the batch.

## Convert one file or a selected batch

`Convert-OfficeDocument` uses the shared OfficeIMO runner. A destination extension can select the single-file target; a batch uses `-To`. General conversion jobs require PowerShell 7.6 or newer.

```powershell
Import-Module PSWriteOffice
$report = Convert-OfficeDocument -Path ./report.pdf -To Word -OutputPath ./report.docx
$report.Diagnostics
Get-ChildItem ./reports -Filter *.pdf |
    Convert-OfficeDocument -OutputDirectory ./word -To Word -NoClobber
```

Use `-NoClobber` to preserve an existing destination or `-AutoRename` for another filename. `-CheckpointPath` and `-Resume` support resumable directory jobs. Retain per-file reports. `Get-OfficeDocumentCapability -Conversion -Executable` lists the current host's routes; their loss policies still apply.

## Run PowerShell notebook cells

`Open-OfficeNotebook` reads a notebook into a live document. Inspect its saved content with `Get-OfficeNotebookCell` and `Get-OfficeNotebookOutput`, use `Export-OfficeNotebook` to publish it, and close the document with `Close-OfficeNotebook`.

Run notebooks whose code you trust. `Invoke-OfficeNotebook` runs PowerShell cells in document order in one session. Select zero-based indexes with `-CellIndex`, set a per-cell limit with `-TimeoutSeconds`, and use `-OutputPath` to save execution counts and outputs in an `.ipynb` file.

```powershell
$report = Invoke-OfficeNotebook -LiteralPath ./lab.ipynb -OutputPath ./executed.ipynb -NoClobber -TimeoutSeconds 30
try {
    $report.Cells
} finally {
    $report.Notebook | Close-OfficeNotebook
}
```

Run `Get-Help about_PSWriteOffice_Notebook` for notebook imports, exports, and saved results.

## Work with native document families

Project tasks, Access rows and EPUB chapters accept ordinary PowerShell parameters and object pipelines. The [native document example](https://github.com/EvotecIT/OfficeIMO/blob/105ae88c45f6be3bb2020384e25ffe61d990451b/PowerShell/PSWriteOffice/Examples/Workflows/Example-NativeDocumentFamilies.ps1) creates and reads these artifacts without constructing .NET option objects.

- Use `New-OfficeProject`, task/resource/assignment commands and `Invoke-OfficeProjectSchedule` to prepare a schedule. XML preserves the supported scheduling model; native MPP saves assess generation-specific loss. Inspect `-ReportVariable` and opt into `-AllowLoss` only after reviewing the reported reductions.
- Use `New-OfficeAccess` and table/column/row commands for a native MDB or ACCDB table database. The file operations remain inert: they do not start ACE, run SQL, or execute database code.
- Use `New-OfficeEpub`, `Add-OfficeEpubChapter` and `Add-OfficeEpubResource` to compose a publication. Plain chapter text is escaped; XHTML and Markdown are explicit alternatives.

## Find exact parameters

The [command reference](/api/powershell/) is the authoritative parameter contract. The [examples](https://github.com/EvotecIT/OfficeIMO/tree/105ae88c45f6be3bb2020384e25ffe61d990451b/PowerShell/PSWriteOffice/Examples) demonstrate complete job shapes. Pin the module version in production automation and review release notes before changing that pin.

<!-- PowerForge markdown source sha256:1579B074FF88AC49C3485AF28B1AFF16D616F64B1D59228E15F6D235B29CE197; title=True -->

<!-- PowerForge markdown artifact sha256:E547338272C46A0716FE1591BC3B06DFCF5F9B7CBE3A9A0484A146B9386B0102 -->
