liveBook · Manning

PowerShell way

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

The PowerShell way is an idiomatic approach to writing scripts that follows PowerShell’s native capabilities and community conventions.

The PowerShell way means using PowerShell as designed rather than reproducing habits from VBScript, C#, or other scripting environments. It follows native capabilities and community best practices, while recognizing that an idiom is preferred—not the only possible solution. section 3.0

Use these principles when turning an automation need into a reusable tool:

  • Start by describing the business task in granular steps before writing code. Separate reusable tools from controllers that coordinate a particular job. section 6.7
  • Write focused functions instead of one large script. Keep functions generic and avoid hardcoded names or paths so they can be reused. section 17.1.1
  • Return objects to the pipeline rather than formatted text. Objects can later become CSV files, HTML reports, or other output. section 17.1.1
  • Prefer native PowerShell commands and make command names, verbs, nouns, prefixes, and parameter names resemble established commands. section 17.1.1 section 8.4
  • Do not optimize raw output for visual ordering. Use formatting commands or Select-Object when presentation requires a particular order. section 12.3

From task to PowerShell design

For a request to find files untouched for more than a year, identify the tool that gathers file paths, sizes, dates, and owners, then have a controller export the resulting objects to CSV. For terminated accounts, identify tools that disable the account, add the termination comment and group membership, and notify the manager. The design matters before the code. section 6.7

OSVersion         : 10.0.22621
SPVersion         : 0
SysDriveFreeSpace : 16500285440
Procs             : 1
Manufacturer      : HyperV
Cores             : 12
ComputerName      : 
RAM               : 31.5475044250488
OSBuild           : 22621
Model             : 20MDCTO1WW
Arch              : 64

A machine-information listing demonstrates the intended separation between object data and its later presentation: properties remain available for commands that select, format, or export them. section 12.3

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Explains design principles and applies them to functions, objects, naming, reuse, and task decomposition, while deferring detailed error handling.
Examples
Uses file-age reporting, terminated-account processing, folder-size functions, machine-information objects, and a rewrite exercise.
What it assumes
Assumes basic PowerShell scripting and teaches the distinction between inherited scripting habits and PowerShell-native design.
What you can do after
Apply it via PowerShell by designing focused tools, composing controllers, returning pipeline objects, and following native naming practices.
How it connects
Connects functions, objects, the pipeline, commands, tools, controllers, naming conventions, parameters, and CSV output.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.