Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Windows PowerShell Scripting (Second Edition)

You're reading from   Mastering Windows PowerShell Scripting (Second Edition) One-stop guide to automating administrative tasks

Arrow left icon
Product type Paperback
Published in Oct 2017
Publisher Packt
ISBN-13 9781787126305
Length 440 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Chris Dent Chris Dent
Author Profile Icon Chris Dent
Chris Dent
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. Introduction to PowerShell FREE CHAPTER 2. Working with PowerShell 3. Modules and Snap-Ins 4. Working with Objects in PowerShell 5. Operators 6. Variables, Arrays, and Hashtables 7. Branching and Looping 8. Working with .NET 9. Data Parsing and Manipulation 10. Regular Expressions 11. Files, Folders, and the Registry 12. Windows Management Instrumentation 13. HTML, XML, and JSON 14. Working with REST and SOAP 15. Remoting and Remote Management 16. Testing 17. Error Handling

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 Integrated Scripting Environment (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 User Settings from File and Preferences. 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.
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:

You have been reading a chapter from
Mastering Windows PowerShell Scripting (Second Edition) - Second Edition
Published in: Oct 2017
Publisher: Packt
ISBN-13: 9781787126305
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime