LOGO

Geek School: Automate Windows with PowerShell

March 25, 2013
Geek School: Automate Windows with PowerShell

PowerShell: A Foundation for IT Professionals

This installment of Geek School focuses on providing a comprehensive understanding of the PowerShell scripting language. It’s natively integrated within the Windows operating system and represents a crucial skill for individuals working in information technology.

Why Learn PowerShell?

Although this learning path isn't tied to a specific certification, mastering PowerShell is arguably one of the most valuable investments an IT professional can make. Developing proficiency in this area can significantly enhance your career prospects.

Beyond its practical applications, PowerShell offers a dynamic and engaging learning experience. Its capabilities extend far beyond basic task automation.

The benefits of learning PowerShell are numerous, and it’s a skill that will continue to be in high demand within the IT sector.

Consider prioritizing PowerShell if you're seeking a single skill to elevate your IT expertise. It’s a powerful tool with a rewarding learning curve.

PowerShell: An Overview

As Microsoft’s premier automation solution, PowerShell functions as both a command-line shell and a robust scripting language.

It provides administrators and advanced users with a comprehensive set of tools for system management and task automation.

Important Considerations

This instructional series is specifically designed around PowerShell 3, the version included with Windows 8 and Server 2012.

Users operating Windows 7 are advised to download and install the PowerShell 3 update prior to proceeding to ensure compatibility and access to all features.

Key Features of PowerShell

  • Automation Capabilities: PowerShell excels at automating repetitive tasks, reducing manual effort and potential errors.
  • Scripting Language: Its scripting functionality allows for the creation of complex workflows and customized solutions.
  • Command-Line Shell: PowerShell provides a powerful command-line interface for direct system interaction.

The combination of these features makes PowerShell an invaluable asset for managing Windows environments effectively.

Understanding its core concepts is crucial for any system administrator or IT professional.

Getting Started

Before diving into advanced scripting, it’s essential to familiarize yourself with the basic syntax and commands.

This series will progressively build your knowledge, starting with fundamental concepts and moving towards more complex scenarios.

Ensure you have the correct PowerShell version installed to avoid any compatibility issues during the learning process.

PowerShell Interaction Methods: Console vs. ISE

PowerShell provides two primary interfaces for user interaction: the Console and the Integrated Scripting Environment (ISE). The ISE has undergone significant improvements since its initial release with PowerShell 2 and can be launched by utilizing the Win + R key combination to access the run dialog. Subsequently, type 'powershell_ise' and press Enter.

geek-school-learn-how-to-automate-windows-with-powershell-1.jpg

The ISE features a divided screen, enabling simultaneous script creation and result observation in the lower pane. This lower section, displaying script outputs, also functions as a Read-Eval-Print Loop (REPL) environment, similar to the command prompt. Version 3 of the ISE introduced IntelliSense support within both the script editor and the interactive console.

geek-school-learn-how-to-automate-windows-with-powershell-2.jpg
geek-school-learn-how-to-automate-windows-with-powershell-3.jpg

Alternatively, users can engage with PowerShell through the PowerShell Console. This interface mirrors the functionality of the command prompt, accepting commands and displaying corresponding outputs. To initiate the Windows PowerShell Console, press Win + R to open the run box, then type 'powershell' and press Enter.

geek-school-learn-how-to-automate-windows-with-powershell-4.jpg

Interactive prompts, such as these, provide immediate feedback: a command is entered, and the results are promptly shown. While the Console lacks IntelliSense, it offers tab completion, which serves a similar purpose. Simply begin typing a command and press the Tab key to cycle through potential matches.

geek-school-learn-how-to-automate-windows-with-powershell-5.jpg
geek-school-learn-how-to-automate-windows-with-powershell-6.jpg

Leveraging the PowerShell Help System

Previous iterations of PowerShell included help files directly with the Windows installation. While generally functional, this approach presented a challenge. A disconnect existed between the PowerShell development cycle and the help file updates. Developers continued to refine the code, but the accompanying help documentation often lagged behind, becoming inaccurate.

PowerShell 3 addresses this issue with a built-in, updatable help system. Unlike earlier versions, it doesn’t ship with pre-installed help files. Therefore, the initial step is to download the most current help information. This is accomplished by initiating a PowerShell Console and executing the following command:

Update-Help

Successfully running this command marks your first interaction with PowerShell! The Update-Help command, however, offers a range of options beyond its basic execution. To explore these options, you can access the command’s help documentation. To view help for any command, simply provide its name to the Name parameter of the Get-Help command, as demonstrated below:

Get-Help –Name Update-Help

Understanding the presented help information can initially seem complex. The syntax section contains two distinct blocks, and numerous brackets appear throughout. These blocks represent different parameter sets, each defining a unique way to invoke the command. Only one parameter set can be used at a time; mixing parameters from different sets is not permitted.

The presence of a SourcePath parameter in the top parameter set, but not the bottom, illustrates this. The top set is utilized when updating help files from a network location where they have already been downloaded. Specifying a source path is unnecessary when retrieving the latest files directly from Microsoft.

Help files adhere to a specific syntax to convey information effectively. Here's a breakdown:

  • Parameters enclosed in square brackets, along with their data type, are optional. The command will function correctly even if these parameters are omitted.
  • Square brackets surrounding only the parameter name indicate a positional parameter.
  • Data types expected by a parameter are indicated within angled brackets to the right of the parameter name.

While familiarizing yourself with this syntax is beneficial, the –Full parameter provides additional clarity. Appending –Full to your Get-Help command reveals a more detailed description of each parameter in the parameters section.

Get-Help –Name Update-Help –Full

The help system also functions as a command discovery tool. PowerShell’s support for wildcards enables you to search for commands based on keywords. For instance, to find commands related to Windows Services:

Get-Help –Name *service*

Although mastering all these details may not be immediate, investing time in learning the help system is invaluable. It proves useful consistently, even for experienced PowerShell scripters with years of practice.

Security Considerations in PowerShell

A discussion of PowerShell wouldn’t be complete without addressing its security aspects. A primary concern for the PowerShell development team is preventing the platform from becoming a favored target for malicious actors and inexperienced hackers.

Several security measures have been implemented to mitigate these risks. Let's examine these protective features in detail.

A foundational layer of security stems from the fact that the .ps1 file extension – identifying PowerShell scripts – is not directly associated with the PowerShell host application.

Instead, it's registered with Notepad. Consequently, double-clicking a .ps1 file will open it within Notepad rather than executing it.

Furthermore, scripts cannot be initiated directly from the PowerShell shell simply by typing their name. The complete path to the script file must be specified for execution.

For example, to run a script located on your C drive, you would enter:

C:\runme.ps1

Alternatively, if you are already positioned at the root of the C drive, the following command can be used:

.\runme.ps1

PowerShell also incorporates Execution Policies, which control the ability to run scripts. By default, script execution is disabled, requiring a modification of the execution policy to permit script execution.

Four key Execution Policies are available:

  • Restricted: This is the default setting. No scripts can be executed, only individual commands.
  • AllSigned: Scripts can run, but they must be digitally signed by a trusted publisher. A prompt will appear before running scripts from trusted sources.
  • RemoteSigned: Scripts are allowed to run, provided that scripts and configuration files downloaded from the internet are digitally signed by a trusted publisher. Locally created scripts do not require signing.
  • Unrestricted: Allows all scripts, including unsigned ones downloaded from the internet, to run. This poses a significant security risk and is not recommended.

To determine your current Execution Policy, open a PowerShell console and type:

Get-ExecutionPolicy

geek-school-learn-how-to-automate-windows-with-powershell-12.jpg

For most use cases, including this course, the RemoteSigned policy offers the optimal balance of security and functionality.

You can adjust your policy using the following command.

Note: This action requires an elevated PowerShell console.

Set-ExecutionPolicy RemoteSigned

geek-school-learn-how-to-automate-windows-with-powershell-13.jpg

This concludes our discussion on security for now. We will continue exploring PowerShell in the next session.

A clarification: The technically correct term for a PowerShell command is a cmdlet. From this point forward, we will utilize this accurate terminology, although "command" was used for introductory clarity.

Should you have any questions, feel free to reach out to me on Twitter @taybgibb, or leave a comment below.

#PowerShell#automation#Windows#Geek School#scripting#system administration