Definition

PowerShell

This definition is part of our Essential Guide: The essential Office 365 migration guide
Contributor(s): Don Jones, Adam Bertram

PowerShell is an object-oriented automation engine and scripting language with an interactive command-line shell that Microsoft developed to help IT professionals configure systems and automate administrative tasks.

Microsoft designed PowerShell to automate system tasks, such as batch processing, and to create systems management tools for commonly implemented processes. The PowerShell language is similar to Perl.

PowerShell, built on the .NET framework, offers several ways to automate tasks:

  • with cmdlets, which are very small .NET classes that appear as system commands;
  • with scripts, which are combinations of cmdlets and associated logic;
  • with executables, which are stand-alone tools; and
  • with the instantiation of standard .NET classes.

Cmdlets

A cmdlet performs a single function and is the most common element for PowerShell automation. A cmdlet is generally written in a language such as C# and compiled. Like all other PowerShell commands, a cmdlet performs an action and returns a .NET object.

Each cmdlet is associated with help content that provides the details of the cmdlet, such as its description, command syntax, description of its parameters and an example. PowerShell includes more than 200 basic cmdlets.

Some of the core cmdlets included in PowerShell are:

Core PowerShell cmdlets

PowerShell modules

PowerShell modules enable administrators to reuse a script to automate a task. A PowerShell module can be defined as a set of PowerShell functions that are grouped to manage all the aspects of a particular area. PowerShell modules enable administrators to reference, load, persist and share code. The simplest way to create a PowerShell module is to save the script as a PSM1 file.

A PowerShell module contains four essential elements:

  • a PSM file, which is the module;
  • help files or scripts needed by the module;
  • a manifest file that describes the module; and
  • a directory that stores the content.

A PowerShell module can be one of four types:

  • Script module: A PSM1 file that contains various functions to enable administrators to perform import, export and management functions.
  • Binary module: A .NET framework assembly (DLL file) that contains compiled code. Developers typically use a binary module to create cmdlets with powerful features not easily done with a PowerShell script.
  • Manifest module: A manifest module is a module (PSM1) file with an associated PSD1 file (manifest).
  • Dynamic module: A dynamic module is created dynamically on demand by a script. It is not stored or loaded to persistent storage.

PowerShell functions

A PowerShell function is similar to a PowerShell cmdlet, with several slight differences. Cmdlets are written in a compiled .NET language, such as C#, while functions are written in PowerShell and are not compiled.

For developers and independent software vendors, it is easier to package and deploy a PowerShell cmdlet compared to packaging libraries of functions. Cmdlets and advanced functions support powerful parameter bindings.

By describing a parameter to the shell, administrators can use any type of PowerShell parameters, such as named parameters, mandatory parameters and positional PowerShell parameters.

An example of a PowerShell function is:

function Set-Something {
  [CmdletBinding()]
  param (
    [Parameter()]
    [string]$Thing
  )

  Write-Host $Thing
}

A function contains one or more option parameters inside of a parameter block and a body.

The history of PowerShell

Microsoft technical fellow Jeffrey Snover developed PowerShell. Before Snover joined Microsoft in 1999, he worked at Digital Equipment Corp. While working on system administration tasks on Windows, Snover felt Windows lacked the power and flexibility to automate tasks available in Unix.

At that time, every Microsoft operating system -- starting with DOS -- had a command-line interface, but it only supported basic commands. It was not robust enough to handle all facets of the GUI functionality.

Microsoft released Windows Script Host with Windows 98 in 1998. The command-line host cscript.exe integrated with Active Directory, which made it possible to write scripts in VBScript or JavaScript. But cscript.exe did not integrate with the shell, and writing code in VBScript was a challenge.

Snover developed the idea to create a standardized platform that used the .NET framework through objects and automation tasks. Microsoft produced a new command-line interface along with a shell called Monad. Microsoft released the first beta Monad on June 17, 2005, followed by a second and third beta on September 11 and January 10, 2006, respectively. On April 25, 2006, Microsoft renamed Monad to Windows PowerShell and made it a core part of the Windows operating system.

Microsoft released PowerShell 1.0 in November 2006 for Windows XP SP2, Windows Vista and Windows Server 2003 SP1. With Windows Server 2008, Microsoft offered PowerShell as an optional component.

Microsoft released PowerShell 2.0 with Windows 7 and Windows Server 2008 R2.

System requirements

Windows PowerShell 5.0 is the latest version installed by default in Windows Server 2016 and Windows 10. Windows 8.1 systems have PowerShell 4.0 installed by default.

Windows PowerShell 5.0 works with Windows 7 SP1, Windows Server 2008 R2 SP1, Windows Server 2012 and Windows Server 2012 R2. These systems need Windows Management Framework 5.0 installed to run PowerShell 5.0.

PowerShell 5.0 requires a full installation of Microsoft .NET framework 4.5.

Key PowerShell features

Microsoft incorporates updates and new features with each PowerShell version. In PowerShell 3.0, Microsoft added:

  • PowerShell workflows written in XAML, or PowerShell that can run as cmdlets.
  • Common Information Model (CIM) cmdlets to manage any device or server that complies with the CIM and WS-Management standards of the Distribution Management Task Force (DMTF).
  • Updated PowerShell remoting to enable an administrator to disconnect a remote session and reconnect to the same session using the same or a different computer.
  • Updatable help to download updated help files from Microsoft servers.
  • Scheduled jobs enable administrators to program a job to run on a timetable. Scheduled jobs exist outside PowerShell and can execute without running PowerShell.
  • Automatic module loading feature loads all commands from all installed modules. When a command is executed in a module that isn't loaded, PowerShell automatically loads it in the background.
  • PowerShell web access enables PowerShell commands and scripts to run with a web browser.

With Windows PowerShell 5.0, Microsoft improved several features:

  • enhanced Desired State Configuration;
  • the ability to define classes in functionality;
  • PowerShellGet package manager, which simplifies the process to discover and install software on a computer;
  • enhanced debugging; and
  • new features in Windows PowerShell ISE and PowerShell Web Service.

Desired State Configuration

With PowerShell 4.0, Microsoft introduced a feature called Desired State Configuration (DSC), which administrators can use to set a specific configuration for a server. After the administrator defines the server settings, PowerShell ensures the target nodes retain that desired state. DSC has two modes of operation: push mode and pull mode.

In push mode, a server sends notifications to the nodes. It is a one-way communication, where the administrator sends notifications from a workstation. Setup costs are less because management runs from a device, but a notification gets lost if the device is not connected to the network.

In push mode, the IT department creates a pull server with the configuration details of each node using an MOF file. Each node contacts the pull server to check for a new configuration. If the new configuration is available, the pull server sends the configuration to the node. Administrators can manage all devices regardless of their network connection. When a device connects to the network, it automatically contacts the pull server to check for a new configuration.

DSC resources

DSC resources are the components of a configuration script. Administrators can check the available DSC resources on a machine with the Get-DscResource command.

Get-DscResource command
The Get-DscResource command retrieves the available Desired State Resources on a machine.

Administrators use these resources to configure components such as registry keys and Windows services, or to create and manage local users through a configuration script. For instance, the File resource manages files and folders, the Environment resource manages environment variables and the Registry resource manages the registry keys of a node.

PowerShell Integrated Scripting Environment

PowerShell Integrated Scripting Environment (ISE), introduced by Microsoft in PowerShell version 2.0, is a PowerShell host application used to write, test and debug scripts or write commands in a Windows GUI.

PowerShell ISE comes with multiple features, such as syntax coloring, multiline editing, context-sensitive help and tab completion. 

PowerShell Integrated Scripting Environment
Microsoft introduced the PowerShell Integrated Scripting Environment with PowerShell version 2.0.

PowerShell ISE has sophisticated features that are familiar to Windows users. For instance, a user can highlight and copy a portion of a PowerShell command with a mouse or with the Shift + Arrow hotkey combination. The user can also paste the content anywhere in the editor window.

Another useful feature is the ability to keep different versions of a command in the editor and run commands that you need in the PowerShell ISE.

The F5 key launches a command directly from the editor. To execute a particular line, select it and press F8. The context-sensitive help displays matching cmdlets when the user starts to enter a command. A command add-on shows a list of cmdlets to select.

PowerShell ISE provides tabs to allow work on multiple administrative tasks. PowerShell ISE enables quick switching from the CLI to scripting mode.

Language constructs

As a scripting language, PowerShell offers several language constructs that control the flow of scripts, while making decisions as to what should be done. Some of the language constructs include conditionals, switches, loops and variables.

Conditionals: The language construct if is used to evaluate a conditional expression. When the conditional expression is true, a script block is executed.

if ($i -eq 1)
{
  ## Do something
}
else
{
  ## Do something else
}

Switch: The switch statement is used when there is a long list of conditional statements to test. Switch is commonly used in place of many if/then/else constructs.

switch ($i) {
  0
  {
    Write-Host "I is 0"
  }
  1
  {
    Write-Host "I is 0"
  }
  Default
  {
    Write-Host "I is not 0 or 1"
  }
}

Loops: The while statement repeats code as long as the following conditional expression is true:

while ($i -eq 0) {
  ## Do somthing
}

The do loop is similar to the while loop. The only difference is PowerShell executes the do loop at the end of the loop.

do {
  ## do something
} while ($i -lt 0)

When you use a foreach loop, PowerShell repeats the code for each item mentioned in the script.

$array = ('item1','item2','item3')
foreach ($item in $array) {
  $item
}

Use a for loop to execute statements repeatedly until a condition is met.

for ($i = 0; $i -lt 5; $i++)
{
  $i
}

Variables

Variables store data, but PowerShell variables are more powerful because they can be mapped to underlying classes in the .NET framework. PowerShell treats variables as objects, which means they can store data and also manipulate data in multiple ways.

Variable names in PowerShell start with a $ sign and contain a mix of numbers, letters, symbols and spaces. For instance, $var="HELLO" stores the string HELLO in the $var variable. Variables can also have different scopes, such as global, local, script, private and numbered scopes.

Arrays

A PowerShell array is a component that enables the storage of more than one item in a variable or a field. For instance, to assign multiple values to a variable, use the following script:

$a=1,2,3

PowerShell treats each item in an array as a separate element. To address each item in an array, PowerShell offers index numbers. The first element in the array is indexed as 0 by default. The biggest advantage of PowerShell is that it automatically handles array insertions, so arrays do not have to be manually destroyed or created when adding or removing elements.

Hash tables

Hash tables are data structures similar to arrays. A PowerShell array stores multiple single items, but with a hash table, each item or value is stored using a key or value pair. An array cannot store multiple values under each element, while a hash table can.

Below is an example comparing an array with a hash table:

$array = @('Joe','Susie','Donnie')
$hashtable = @{FirstName = 'Joe'; FirstName = 'Susie'; FirstName = 'Donnie'}

Help and comments

PowerShell enables the addition of help topics for modules, scripts and individual commands. To view all the help topics, use the Get-Help command.

When importing a module into a session, PowerShell automatically imports the help topics for that module. If there are no help topics for a module, the Get-Help command displays autogenerated help. Three types of help content exist in PowerShell: comment-based help, external help and updatable help.

Comment-based help refers to comments included with a script or command for Get-Help to read. External help enables the author to define help content in an external XML file written in XAML. Updatable help uses external help, but enables users to download the latest help content with the Update-Help command.

Executable programs

PowerShell is also a cmd.exe replacement that runs an executable program in multiple ways through the Start-Process command, the ampersand and the Invoke-Expression command. Using ping.exe as an example, here's how a user can run the executable program using PowerShell.

Start-Process -FilePath 'ping.exe' -ArgumentList 'google.com' -Wait -NoNewWindow
& 'ping.exe' google.com
ping google.com
Invoke-Expression -Command 'ping.exe google.com'

This was last updated in June 2017

Continue Reading About PowerShell

PRO+

Content

Find more PRO+ content and other member only offers, here.

Join the conversation

2 comments

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

Where have you found the most benefit to using PowerShell in your environment?
Cancel
Huh?
Cancel

-ADS BY GOOGLE

File Extensions and File Formats

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchEnterpriseDesktop

SearchVirtualDesktop

Close