Getting Started with PowerShell

Book description

Learn the fundamentals of PowerShell to build reusable scripts and functions to automate administrative tasks with Windows

About This Book

  • Harness the capabilities of the PowerShell system to get started quickly with server automation
  • Learn to package commands into a reusable script and add control structures and parameters to make them flexible
  • Get to grips with cmdlets that allow you to perform administration tasks efficiently

Who This Book Is For

This book is intended for Windows administrators or DevOps users who need to use PowerShell to automate tasks. Whether you know nothing about PowerShell or know just enough to get by, this guide will give you what you need to go to take your scripting to the next level.

What You Will Learn

  • Learn to verify your installed version of PowerShell, upgrade it, and start a PowerShell session using the ISE
  • Discover PowerShell commands and cmdlets and understand PowerShell formatting
  • Use the PowerShell help system to understand what particular cmdlets do
  • Utilise the pipeline to perform typical data manipulation
  • Package your code in scripts, functions, and modules
  • Solve common problems using basic file input/output functions
  • Find system information with WMI and CIM
  • Automate IIS functionality and manage it using the WebAdministration module

In Detail

Windows PowerShell is a task-based command-line shell and scripting language designed specifically for system administration. Built on the .NET Framework, Windows PowerShell helps IT professionals and power users control and automate the administration of the Windows operating system and applications that run on Windows.

PowerShell is great for batch importing or deleting large sets of user accounts and will let you collect a massive amount of detailed system information in bulk via WMI (Windows Management Instrumentation).

Getting Started with PowerShell is designed to help you get up and running with PowerShell, taking you from the basics of installation, to writing scripts and web server automation. This book, as an introduction to the central topics of PowerShell, covers finding and understanding PowerShell commands and packaging code for reusability, right through to a practical example of automating IIS. It also includes topics such as installation and setup, creating scripts, automating tasks, and using Powershell to access data stores, registry, and file systems.

You will explore the PowerShell environment and discover how to use cmdlets, functions, and scripts to automate Windows systems. Along the way, you will learn to perform data manipulation and solve common problems using basic file input/output functions.

By the end of this book, you will be familiar with PowerShell and be able to utilize the lessons learned from the book to automate your servers.

Style and approach

A practical learning guide, complete with plenty of activities, examples and screenshots.

Table of contents

  1. Getting Started with PowerShell
    1. Table of Contents
    2. Getting Started with PowerShell
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. First Steps
      1. Determining the installed PowerShell version
        1. Using the registry to find the installed version
        2. Using PowerShell to find the installed version
      2. Installing/upgrading PowerShell
      3. Starting a PowerShell session
        1. PowerShell hosts
        2. 64-bit and 32-bit PowerShell
        3. PowerShell as an administrator
      4. Simple PowerShell commands
      5. PowerShell aliases
      6. Summary
      7. For further reading
    9. 2. Building Blocks
      1. What can you do?
      2. The scripter's secret weapon – tab completion
      3. How does that work?
      4. Interpreting the cmdlet syntax
      5. Summary
      6. For further reading
    10. 3. Objects and PowerShell
      1. Objects all the way down
        1. Digging into objects
        2. Types, classes, and objects
      2. What are members?
        1. The DOS DIR command
        2. The IPCONFIG command
        3. PowerShell for comparison
      3. The Get-Member cmdlet
      4. Where did these all come from?
      5. Summary
      6. For further reading
    11. 4. Life on the Assembly Line
      1. The pipeline as an assembly line
        1. This isn't your DOS or Linux pipeline
        2. Objects at your disposal
      2. Dealing with pipeline data
        1. The Sort-Object cmdlet
        2. The Where-Object cmdlet
        3. The Select-Object cmdlet
          1. Limiting the number of objects returned
          2. Limiting the properties of the objects returned
          3. Retrieving the values of a single property
        4. The Measure-Object cmdlet
        5. The Group-Object cmdlet
      3. Putting them together
      4. Summary
      5. For further reading
    12. 5. Formatting Output
      1. When does formatting occur?
      2. The rules of automatic formatting
        1. Formatting files
        2. Formatting decisions are based on the first object
        3. Small objects go in a table
        4. Large objects go in a list
      3. Cmdlets that control formatting
        1. Format-Table and Format-List
      4. The dangers of formatting
      5. Best practices of formatting
      6. Summary
      7. For further reading
    13. 6. Scripts
      1. Packaging commands
        1. Execution policy
        2. Types of scripts
        3. Scopes and scripts
      2. Parameters add flexibility
      3. Adding some logic
        1. Conditional logic (IF)
        2. Looping logic
        3. More logic
      4. Profiles
      5. Summary
      6. For further reading
    14. 7. Functions
      1. Another kind of container
        1. Comparing scripts and functions
      2. Executing and calling functions
      3. Naming conventions
      4. Comment-based help
      5. Parameters revisited
        1. Typed parameters
        2. Switches
      6. Default values for parameters
      7. Output
      8. Summary
      9. For further reading
    15. 8. Modules
      1. Packaging functions
      2. Script modules
        1. The Export-ModuleMember cmdlet
      3. Where do modules live?
      4. Removing a module
      5. PowerShell module autoloading
      6. The #Requires statement
      7. Removing a module – take two
        1. Manifest modules
      8. Listing modules
      9. Summary
    16. 9. File I/O
      1. Reading and writing text files
        1. Writing text files
      2. Working with CSV files
        1. Output to CSV for quick reports
        2. The Invoke-Item cmdlet
        3. Import from CSV for quick objects
      3. PowerShell streams and redirection
        1. Other types of redirection operators
        2. The out-file cmdlet
      4. CLIXML – a special type of XML
      5. Summary
      6. For further reading
    17. 10. WMI and CIM
      1. What is WMI?
        1. WMI organization
      2. Finding WMI classes
      3. Retrieving objects with Get-WMIObject
        1. Getting the right instance
        2. WQL syntax
      4. Calling methods
      5. WMI and CIM
        1. The CIM cmdlets
      6. CDXML modules
      7. Summary
      8. For further reading
    18. 11. Web Server Administration
      1. Installing IIS
        1. Detecting and installing IIS in Windows 8.1
        2. Detecting and installing IIS in Server 2012R2
      2. Verifying IIS
      3. The WebAdministration module
      4. Starting, stopping, and restarting IIS
      5. Creating virtual directories and web applications
      6. Working with application pools
        1. Creating application pools
      7. Summary
      8. For further reading
    19. A. Next Steps
    20. Index

Product information

  • Title: Getting Started with PowerShell
  • Author(s): Michael Shepard
  • Release date: August 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781783558506