Learn PowerShell Scripting in a Month of Lunches
Don Jones and Jeffery Hicks
  • November 2017
  • ISBN 9781617295096
  • 352 pages
  • printed in black & white
ePub + Kindle available Nov 30, 2017

A very clear and concise depiction of the best parts of PowerShell.

Justin Coulston, Intellectual Technology


An eBook copy of the previous edition, Learn PowerShell Toolmaking in a Month of Lunches, is included at no additional cost. It will be automatically added to your Manning account within 24 hours of purchase.

Discover how scripting is different from command-line PowerShell, as you explore concrete hands-on examples in this handy guide. The book includes and expands on many of the techniques presented in Learn PowerShell Toolmaking in a Month of Lunches.

Table of Contents detailed table of contents

Part 1: Introduction to Scripting

1. Before you begin

1.1. What is toolmaking?

1.2. Is this book for you?

1.3. Here’s what you need to have

1.3.1. PowerShell version

1.3.2. Administrative privileges

1.3.3. SQL Server

1.3.4. Script editor

1.4. How to use this book

1.5. How to ask for help

1.6. Summary

2. Setting up your scripting environment

2.1. The Operating System

2.2. Windows PowerShell

2.3. Administrative Privileges and Execution Policy

2.4. A Script Editor

2.5. Setup a Virtual Environment

2.6. Example Code

2.7. Your Turn

3. WWPD: What would PowerShell do?

3.1. Writing Single-Task Tools

3.2. Naming Tools

3.3. Naming Parameters

3.4. Producing Output

3.5. Don’t Assume

3.6. Avoid Innovation

3.7. Summary

4. Review: Parameter Binding and the PowerShell Pipeline

4.1. Visualizing the Pipeline

4.2. It’s all in the parameters

4.3. Plan A: ByValue

4.3.1. Introducing Trace-Command

4.3.2. Tracing ByValue Parameter Binding

4.3.3. When ByValue Fails

4.4. ByPropertyName

4.4.1. Let’s trace ByPropertyName

4.4.2. When ByPropertyName fails

4.4.3. Planning Ahead

4.5. Summary

5. Scripting Language Crash Course

5.1. The If Construct

5.2. The ForEach Construct

5.3. The Switch Construct

5.4. The Do/While Construct

5.5. The For Construct

5.6. Break

5.7. Summary

6. The many forms of scripting (and which to use)

6.1. Tools vs. controllers

6.2. Thinking about tools

6.3. Thinking about controllers

6.4. Comparing tools and controllers

6.5. Summary

7. Scripts and Security

7.1. PowerShell’s Script Security Goal

7.2. Execution Policy

7.2.1. Execution Scope

7.2.2. Getting Your Policies

7.2.3. Setting an Execution Policy

7.3. PowerShell isn’t the Default Application

7.4. Running Scripts

7.5. Recommendations

7.6. Summary

Part 2: Building a PowerShell Script

8. Always Design First

8.1. Tools Do One Thing

8.2. Tools are Testable

8.3. Tools are Flexible

8.4. Tools Look Native

8.5. For Example

8.6. Your Turn

8.6.1. Start Here

8.6.2. Your Task

8.6.3. Our Take

9. Avoid Bugs: Start with a Command

9.1. What We Need to Run

9.2. Breaking it Down and Running it Right

9.3. Running Commands and Digging Deeper

9.4. Process Matters

9.5. Your Turn

9.5.1. Start Here

9.5.2. Your Task

9.5.3. Our Take

10. Build a Basic Function and Script Module

10.1. Start with a Basic Function

10.1.1. Design the Input Parameters

10.1.2. Write the Code

10.1.3. Design the Output

10.2. Create a Script Module

10.3. Pre-Req Check

10.4. Running the Command

10.5. Your Turn

10.5.1. Start Here

10.5.2. Your Task

10.5.3. Our Take

11. Going Advanced With Your Function

11.1. About CmdletBinding and Common Parameters

11.1.1. Accepting Pipeline Input

11.1.2. Mandatory-ness

11.1.3. Parameter Validation

11.1.4. Parameter Aliases

11.1.5. Your Turn

12. Objects: The Best Kind of Output

12.1. Assembling the Information

12.2. Constructing and Emitting Output

12.3. A Quick Test

12.4. An Object Alternative

12.5. Your Turn

12.5.1. Start Here

12.5.2. Your Task

12.5.3. Our Take

13. Using All the Pipelines

13.1. Knowing the Six Channels

13.2. Adding Verbose and Warning Output

13.3. Doing More with Verbose

13.4. Information Output

13.4.1. A Detailed Information Example

13.5. Your Turn

13.5.1. Start Here

13.5.2. Your Task

13.5.3. Our Take

14. Simple Help: Make a Comment

14.1. Where to Put Your Help

14.2. Getting Started

14.3. Going Further with Comment-Based Help

14.4. Broken Help

14.5. Beyond Comments

14.6. Your Turn

14.6.1. Start Here

14.6.2. Your Task

14.6.3. Our Take

15. Dealing with Errors

15.1. Understanding Errors and Exceptions

15.2. Bad Handling

15.3. Two Reasons for Exception Handling

15.4. Handling Exceptions in Our Tool

15.5. Capturing the Actual Exception

15.6. Handling Exceptions for Non-Commands

15.7. Going Further with Exception Handling

15.8. Deprecated Exception Handling

15.9. Your Turn

15.9.1. Start Here

15.9.2. Your Task

15.9.3. Our Take

16. Filling Out a Manifest

16.1. Module execution order

16.2. Creating a new manifest

16.3. Examining the manifest

16.3.1. Metadata

16.3.2. The root module

16.3.3. Prerequisites

16.3.4. Scripts, types, and formats

16.3.5. Exporting members

16.4. Your Turn

16.4.1. Start Here

16.4.2. Your Task

16.4.3. Our Take

Part 3: Grown-Up Scripting

17. Changing Your Brain When It Comes to Scripting

17.1. Example 1

17.1.1. The Critique

17.1.2. Our Take

17.2. Example 2

17.2.1. The walkthrough

17.2.2. Our take

17.3. Your Turn

17.3.1. Start Here

17.3.2. Your Task

17.3.3. Our Take

17.4. Think Beyond the Literal

18. Professional-grade scripting

18.1. Use source control

18.2. Spell it out

18.3. Comment your code

18.4. Format your code

18.5. Use meaningful non-Hungarian variable names

18.6. Avoid aliases

18.7. Avoid awkward pipelines

18.8. Provide help

18.9. Avoid Write-Host and Read-Host

18.10. Stick with single quotes

18.11. Don’t pollute the global scope

18.12. Be Flexible

18.13. Be Secure

18.14. Strive for Elegance

18.15. Summary

19. An Introduction to Source Control with Git

19.1. Why Source Control?

19.2. What is git?

19.2.1. Installation

19.2.2. Git Basics

19.3. Repository Basics

19.3.1. Creating a Repository

19.3.2. Staging a change

19.3.3. Committing a change

19.3.4. Rolling back a change.

19.3.5. Branching and Merging

19.4. Using git with VSCode

19.5. Integrating with GitHub

19.6. Try It

19.7. Summary

20. Pestering your script

20.1. The vision

20.2. The problems with manual testing

20.3. The benefits of automated testing

20.4. Introducing Pester

20.5. Coding to be tested

20.6. What do you test?

20.6.1. Integration tests

20.6.2. Unit tests

20.6.3. Don’t test what isn’t yours

20.7. Writing a basic Pester test

20.7.1. Creating a fixture

20.7.2. Writing the first test

20.7.3. Creating a mock

20.7.4. Adding more tests

20.7.5. Code coverage

20.8. Summary

21. Signing Your Script

21.1. Why Sign Your Scripts?

21.2. A Word on Certificates

21.3. Set Your Policy

21.4. Code Signing Basics

21.4.1. Getting a Code Signing Certificate

21.4.2. Trusting Self-Signed Certificates

21.4.3. Signing Your Scripts

21.4.4. Testing Script Signatures

21.5. Summary

22. Publishing your script

22.1. Why publish?

22.3. Other publishing targets

22.4. Before you publish

22.4.1. Are you re-inventing the wheel?

22.4.2. Update your manifest

22.4.3. et an API Key

22.5. Ready, Set, Publish

22.5.1. Managing revisions

22.6. Publishing Scripts

22.6.1. Creating ScriptFileInfo

22.6.2. Publishing the script

22.6.3. Managing Published Scripts

22.7. Summary

Part 4: Advanced Techniques

23. Squashing bugs

23.1. There are three kinds of bugs

23.2. Dealing with syntax bugs

23.3. Dealing with results bugs

23.4. Dealing with logic bugs

23.4.1. Setting breakpoints

23.4.2. Setting watches

23.4.3. So much more

23.4.4. Don’t be lazy

23.5. Your turn

23.5.1. Start here

23.5.2. Your task

23.5.3. Our take

24. Making script output prettier

24.1. Start Here

24.2. Creating a default view

24.2.1. Exploring Microsoft’s views

24.2.2. Adding a custom type name to your output objects

24.2.3. Creating a new view file

24.2.4. Adding the view file to a module

24.3. Your Turn

24.3.1. Start Here

24.3.2. Your Task

24.3.3. Our Take

25. Wrapping up the .NET Framework

25.1. Why does PowerShell exist?

25.2. A crash course in .NET

25.3. Exploring a Class

25.4. Making a Wrapper

25.5. A more practical example

25.6. Your Turn

25.6.1. Start Here

25.6.2. Your Task

25.6.3. Our Take

26. Storing data — not in Excel!

26.1. Introducing: SQL Server!

26.2. Setting everything up

26.3. Using your database: creating a table

26.4. Saving data to SQL Server

26.5. Querying data from SQL Server

26.6. Summary

27. Never the end

27.1. Welcome to toolmaking

27.2. Take your next step

27.3. What’s in your future?

About the Technology

Automate it! With Microsoft's PowerShell language, you can write scripts to control nearly every aspect of Windows. Just master a few straightforward scripting skills, and you'll be able to eliminate repetitive manual tasks, create custom reusable tools, and build effective pipelines and workflows. Once you start scripting in PowerShell, you'll be amazed at how many opportunities you'll find to save time and effort.

About the book

Learn PowerShell Scripting in a Month of Lunches teaches you how to expand your command-line PowerShell skills into effective scripts and tools. In 27 bite-size lessons, you'll discover instantly useful techniques for writing efficient code, finding and squashing bugs, organizing your scripts into libraries, and much more. Advanced scripters will even learn to access the .NET Framework, store data long term, and create nice user interfaces.

What's inside

  • Designing functions and scripts
  • Effective pipeline usage
  • Dealing with errors and bugs
  • Professional-grade scripting practices

About the reader

Written for devs and IT pros comfortable with PowerShell and Windows.

About the author

Don Jones is a PowerShell MVP, speaker, and trainer who has written dozens of books on information technology topics. Jeffery Hicks is a PowerShell MVP and an independent consultant, trainer, and author. Don and Jeff coauthored Manning's Learn Windows PowerShell in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches, and PowerShell in Depth.

combo $44.99 pBook + eBook + liveBook
eBook $35.99 pdf + ePub + kindle + liveBook

FREE domestic shipping on three or more pBooks

A great resource for those who want to create scripts for task automation.

Bruno Sonnino, Revolution Software

Teaches you how to become an informed expert in PowerShell scripting.

Shankar Swamy, Stealth Mode Start-up

Real-world examples, best practices, and tips from two of the most respected PowerShell MVPs.

Roman Levchenko, Microsoft MVP

It makes you stop and think, not just 'read and nod.'

Reka Horvath, Wirecard CEE