Book Image

Mastering Windows PowerShell Scripting (Second Edition) - Second Edition

By : Brenton J.W. Blawat
Book Image

Mastering Windows PowerShell Scripting (Second Edition) - Second Edition

By: Brenton J.W. Blawat

Overview of this book

PowerShell scripts offer a handy way to automate various chores. Working with these scripts effectively can be a difficult task. This comprehensive guide starts from scratch and covers advanced-level topics to make you a PowerShell expert. The first module, PowerShell Fundamentals, begins with new features, installing PowerShell on Linux, working with parameters and objects, and also how you can work with .NET classes from within PowerShell. In the next module, you’ll see how to efficiently manage large amounts of data and interact with other services using PowerShell. You’ll be able to make the most of PowerShell’s powerful automation feature, where you will have different methods to parse and manipulate data, regular expressions, and WMI. After automation, you will enter the Extending PowerShell module, which covers topics such as asynchronous processing and, creating modules. The final step is to secure your PowerShell, so you will land in the last module, Securing and Debugging PowerShell, which covers PowerShell execution policies, error handling techniques, and testing. By the end of the book, you will be an expert in using the PowerShell language.
Table of Contents (24 chapters)
Title Page
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

PowerShell editors


While it is possible to write for PowerShell using the Notepad application alone, it is rarely desirable. Using an editor designed to work with PowerShell can save a lot of time.

Specialized PowerShell editors, at a minimum, offer automatic completion (IntelliSense) that reduces the amount of cross-referencing required while writing code. Finding a comfortable editor early is a good way to ease into PowerShell: memorizing commands and parameters is not necessary.

Two editors are discussed, as follows:

  • PowerShell ISE
  • Visual Studio Code

PowerShell ISE

PowerShell IntegratedScriptingEnvironment (ISE) was introduced with PowerShell 2 in October 2009 and has been updated with every subsequent release.

ISE has an immediate advantage over other editors. It is installed along with PowerShell itself and is likely to be available in some form wherever PowerShell is. ISE consists of a text editor pane and a script pane, as shown in the following screenshot:

Features

ISE is a rich editing environment that includes IntelliSense, built-in help, syntax checking, debugging, and so on.

Additional features are available for ISE from the following add-on tools website:

http://social.technet.microsoft.com/wiki/contents/articles/2969.windows-powershell-ise-add-on-tools.aspx

If you are developing code for use on production systems, I strongly recommend adding PS Script Analyzer. PS Script Analyzer will highlight areas of your code that do not conform to its rule set; for example, using an alias instead of a command name would be highlighted.

Community and commercial add-ons can greatly extend the functionality of ISE to simplify day-to-day use.

Installing ISE Preview

In PowerShell 5, the distribution model for ISE is in the process of changing. Until version 5, ISE was released as a part of the Windows Management Framework (WMF). New features were introduced with each version of WMF, but the time between the versions was long.

ISE Preview may be installed from the PowerShell gallery using the following command:

Install-Module -Name PowerShellISE-Preview

Once installed, the update-module command may be used to bring ISE up to par with the published version.

ISE Preview can coexist with the version of ISE installed by the WMF package.

Starting ISE

ISE may be started from the start menu; however, running the powershell_ise command (from the Run dialog, cmd, or the search box) is sufficient. In PowerShell, the simpler ise command is aliased to powershell_ise.exe.

If the preview version from the PowerShell gallery is being used, the following command will start that version of ISE:

Start-ISEPreview 

This first preview version differs a little from the version of ISE shipping with WMF 5. If the distribution model is successful, the PowerShell team hopes to release a new version of ISE every month, with each release fixing bugs and/or adding new features.

Visual Studio Code

Visual Studio Code is a free open source editor published by Microsoft. VS Code may be downloaded from http://code.visualstudio.com.

VS Code is a good choice of editor when working with multiple languages or when specifically looking for an editor that supports Git in a simple manner.

Features

VS Code does not come with the native PowerShell support. It must be added. Once VS Code is installed, open it, and select the EXTENSIONS button on the left-hand side.

Type PowerShell in the search dialog box, and install the PowerShell language support:

After installation, the extension provides Syntax highlighting, testing using PowerShell Script Analyzer, debugging, and so on.

Console

Unlike ISE, the console (or terminal, as it is named) in VS Code must be configured. By default, the terminal in code uses cmd.exe.

The following process is used to make the terminal use PowerShell:

  1. Open UserSettings from FileandPreferences. The same may be achieved by pressing F1 and typing user settings followed by return.
  2. This opens two windows: a Default Settings file on the left and a settings.json on the right. The file on the right holds user-specific configuration that overrides or adds to the default.
  3. Expand the Integrated Terminal section in Default Settings (by clicking on the o9 symbol) to show the default values.
  4. On the right-hand side, enter the following between the curly braces:
"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe" 
  1. Save the changes, then press Ctrl+Shift+' (apostrophe) to open a new PowerShell terminal. Ctrl + ' (apostrophe) toggles the visibility of the terminal window.

Note

This is not ISE In PowerShell ISE, F5 + F8 may be used to execute a script. This is not the case in VS Code. A selection may be executed by pressing F1 and typing run selected to filter options to Terminal: Run Selected Text in Active Terminal.

Version control (Git)

Visual Studio Code comes with integrated support for Git version control. Git is a distributed version control system; each developer has a copy of the same repository.

Setting up a repository is simple, as follows:

  1. Open a folder that contains a project. Then, select the Git button (or press Ctrl +Shift +G).
  2. Click on Initialize git repository button as shown in the following screenshot:
  1. Once you have done this, files may be added to version control when committing (applying a change).

Subsequent changes to files may be inspected before committing again: