Whitepaper : The Guide to Learning PowerShell

  • Learn How To Start Using PowerShell
  • Explore Example Scripts
  • View Advanced Topics
Presenter: Tobias Weltner
Share This:  Facebook Twitter LinkedIn Google+

Learn how to start using PowerShell with this ebook. It covers almost any question you may have regarding PowerShell and its applications. This guide to learning PowerShell presents a wide range of topics which will leave you with an abundance of knowledge.

The covered topics are: The power console; interactive PowerShell; variables; arrays and hashtables; the PowerShell pipeline; working with objects; conditions; loops; functions; scripts; error handling; managing scope; text and regular expressions; XML; working with the file system; managing Windows Registry; processes, services, and event logs; WMI Windows Management Instrumentation; user management; and loading .NET libraries and compiling code.

Part 1

Chapter 1. The Power Console

Welcome to PowerShell! This chapter will introduce you to the PowerShell console and show you how to configure it, including font colors and sizes, editing and display options.

Chapter 2. Interactive PowerShell

PowerShell has two faces: interactivity and script automation. In this chapter, you will first learn how to work with PowerShell interactively. Then, we will take a look at PowerShell scripts.

Chapter 3. Variables

It is time to combine commands whenever a single PowerShell command can’t solve your problem. One way of doing this is by using variables. PowerShell can store results of one command in a variable and then pass the variable to another command. In this chapter, we’ll explain what variables are and how you can use them to solve more complex problems.

Chapter 4. Arrays and Hashtables

Whenever a command returns more than one result, PowerShell will automatically wrap the results into an array. So dealing with arrays is important in PowerShell. In this chapter, you will learn how arrays work. We will cover simple arrays and also so-called “associative arrays,” which are also called “hash tables.

Chapter 5. The PowerShell Pipeline

The PowerShell pipeline chains together a number of commands similar to a production assembly. So, one command hands over its result to the next, and at the end, you receive the result.

Chapter 6. Working with Objects

In this chapter, you will learn what objects are and how to get your hands on PowerShell objects before they get converted to simple text.

Part 2

Chapter 7. Conditions

Conditions are what you need to make scripts clever. Conditions can evaluate a situation and then take appropriate action. There are a number of condition constructs in the PowerShell language which that we will look at in this chapter. In the second part, you’ll employ conditions to execute PowerShell instructions only if a particular condition is actually met.

Chapter 8. Loops

Loops repeat PowerShell code and are the heart of automation. In this chapter, you will learn the PowerShell loop constructs.

Chapter 9. Functions

Functions work pretty much like macros. As such, you can attach a script block to a name to create your own new commands. Functions provide the interface between your code and the user. They can define parameters, parameter types, and even provide help, much like cmdlets. In this chapter, you will learn how to create your own functions.

Chapter 10. Scripts

PowerShell can be used interactively and in batch mode. All the code that you entered and tested interactively can also be stored in a script file. When you run the script file, the code inside is executed from top to bottom, pretty much like if you had entered the code manually into PowerShell. So script files are a great way of automating complex tasks that consist of more than just one line of code. Scripts can also serve as a repository for functions you create, so whenever you run a script, it defines all the functions you may need for your daily work.

Chapter 11. Error Handling

When you design a PowerShell script, there may be situations where you cannot eliminate all possible runtime errors. If your script maps network drives, there could be a situation where no more drive letters are available, and when your script performs a remote WMI query, the remote machine may not be available. In this chapter, you learn how to discover and handle runtime errors gracefully.

Chapter 12. Managing Scope

Anything you define in PowerShell - variables, functions, or settings - have a certain life span. Eventually, they expire and are automatically removed from memory. This chapter talks about “scope” and how you manage the life span of objects or scripts. Understanding and correctly managing scope can be very important. You want to make sure that a production script is not negatively influenced by “left-overs” from a previous script. Or you want certain PowerShell settings to apply only within a script. Maybe you are also wondering just why functions defined in a script you run won’t show up in your PowerShell console. These questions all touch “scope”. At the end of this chapter, we will also be looking at how PowerShell finds commands and how to manage and control commands if there are ambiguous command names.

Chapter 13. Text and Regular Expressions

Often, you need to deal with plain text information. You may want to read the content from some text file and extract lines that contain a keyword, or you would like to isolate the file name from a file path. So while the object-oriented approach of PowerShell is a great thing, at the end of a day most useful information breaks down to plain text. In this chapter, you’ll learn how to control text information in pretty much any way you want.

Chapter 14. XML

In today’s world, data is no longer presented in plain-text files. Instead, XML (Extensible Markup Language) has evolved to become a de facto standard because it allows data to be stored in a flexible yet standard way. PowerShell takes this into account and makes working with XML data much easier than before.

Part 3

Chapter 15. Working with the File System

Working with files and folders is traditionally one of the most popular areas for administrators. PowerShell eases transition from classic shell commands with the help of a set of predefined "historic" aliases and functions. So, if you are comfortable with commands like "dir" or "ls" to list folder content, you can still use them. Since they are just aliases - references to PowerShell’s own cmdlets - they do not necessarily work exactly the same anymore, though. In this chapter, you'll learn how to use PowerShell cmdlets to automate the most common file system tasks.

Chapter 16. Managing Windows Registry

Thanks to PowerShell's universal "Provider" concept, you can navigate the Windows Registry just as you would the file system. In this chapter, you will learn how to read and write Registry keys and Registry values.

Chapter 17. Processes, Services, and Event Logs

In your daily work as an administrator, you will probably often deal with applications (processes), services, and event logs so let's take some of the knowledge you gained from the previous chapters and play with it. The examples and topics covered in this chapter are meant to give you an idea of what you can do. By no means are they a complete list of what you can do. They will provide you with a great starting point, though.

Chapter 18. WMI: Windows Management Instrumentation

Windows Management Instrumentation (WMI) is a technique available on all Windows systems starting with Windows 2000. WMI can provide you with a wealth of information about the Windows configuration and setup. It works both locally and remotely, and PowerShell makes accessing WMI a snap.

Chapter 19. User Management

User administration in the Active Directory was a dark spot in PowerShell Version 1. Microsoft did not ship any cmdlets to manage AD user accounts or other aspects in Active Directory. That's why the 3rd party vendor Quest stepped in and published a free PowerShell Snap-In with many useful AD cmdlets. Over the years, this extension has grown to become a de-facto standard.

Chapter 20. Loading .NET Libraries and Compiling Code

Since PowerShell is layered on the .NET Framework, you already know from Chapter 6 how you can use .NET code in PowerShell to make up for missing functions. In this chapter, we’ll take up this idea once again. You’ll learn about the options PowerShell has for creating command extensions on the basis of the .NET Framework. You should be able to even create your own cmdlets at the end of this chapter.

Register to read the full whitepaper.

Register for Free Whitepaper

Register For a Free Whitepaper