Getting Started with PowerShell

PowerShell has been integrated with the Windows operating system since Windows 7 and Windows Server 2008 R2, so it isn’t necessary to download and install separately. It can also be installed on Windows XP with Service Pack 3, Windows Server 2003 with Service Pack 1 and Windows Vista, as well as it being an optional component of the first release of Windows Server 2008.

In order to work with PowerShell, two applications are provided, one is purely a command line application and the other is PowerShell ISE (Integrated Scripting Environment), where the application window is split in two, with the bottom half being a command line interface and the top half is for creating and editing scripts.

As well as PowerShell ISE, there are also a number of third party pieces of software that can be used for creating and editing PowerShell scripts.

Code Commenting

When programming in any language it is useful to add comments to the code to describe what is happening and why. This is useful when the code is revisited at a later date to make enhancements, or for debugging purposes. In order to add a comment in PowerShell a line needs to start with a ‘#’ symbol as shown below.

# This is a comment in PowerShell.

First PowerShell Script

The PowerShell code written below simply displays the words ‘Hello World!’ in a console or command line window.

Write-Host "Hello World!"

In PowerShell, as with most programming and scripting languages, there are certain words that have a specific purpose and cannot be used for anything else. These are sometimes referred to as reserved words and are shown in blue above.  In PowerShell, where these are hyphenated, they are known as cmdlets, pronounced command-lets, and perform a specific action. In this instance, ‘Write-Host’ causes the words ‘Hello World!’ to be displayed in the console or command line interface.

As well as online help with writing PowerShell scripts, there is also a help feature built in. Note that, before starting to write scripts and then periodically thereafter, it is a good idea to update the help information, which can be done from the command line.

Update-Help

The built-in help information can be accessed from the command line, using the ‘Get-Help’ cmdlet.

Get-Help Get-ChildItem

After executing the above, the help information for the ‘Get-ChildItem’ cmdlet will be displayed.

If examples are available for a particular cmdlet, the 'Examples' parameter can be specified with 'Get-Help' to show these.

Get-Help Get-ChildItem -Examples

Help information can also be viewed online by including the 'Online' parameter.

Get-Help Get-ChildItem -Online

If desired, the help information can be output to a new window using the 'ShowWindow' parameter.

Get-Help Get-ChildItem -ShowWindow

It is also possible to list all cmdlets, functions, and aliases installed on the computer.

Get-Command

As this is likely to produce quite a long list, the results can be filtered, for example, all commands with 'export' in the name.

Get-Command -Name *export*

With PowerShell it is possible to have the output of one command, being the input to another by using the pipe ('|') symbol. Here, the result of the 'Get-Command' cmdlet is output to a text file using the 'Out-File' cmdlet.

Get-Command -Name *export* | Out-File C:\temp\export-cmdlets.txt

Alternatively, the results could be output to an HTML file using the 'ConvertTo-HTML' cmdlet.

Get-Command -Name *export* | ConvertTo-HTML > C:\temp\export-cmdlets.html

If all the properties of the cmdlets are not needed in the output, then these can be filtered.

Get-Command -Name *export* | ConvertTo-HTML -Property Name, HelpUri > C:\temp\export-cmdlets.html

Running a PowerShell Script

The ability to run a PowerShell script is disabled by default. This can be enabled for the local machine using the ‘Set-ExecutionPolicy’ cmdlet in a command line interface as follows.

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

A script can be run a number of different ways. If PowerShell ISE is being used, then clicking on the ‘Run Script’ button will execute the current script. From the command line, it is simply a case of typing the path and file name.

C:\HelloWorld.ps1

If the script resides in the current directory, then there is an alternative way to run it from the command line.

.\HelloWorld.ps1

It is also possible to right click on the script in Explorer and select ‘Run with PowerShell’, however, the console window closes quickly after the script is run, so there is little time to see any feedback.

Once a script has been run, the ability to execute a PowerShell script can be disabled again using the same ‘Set-ExecutionPolicy’ cmdlet.

Set-ExecutionPolicy -ExecutionPolicy Restricted

The cmdlet 'Get-ExecutionPolicy' can be used to check the current state of the execution policy.

Get-ExecutionPolicy

As an alternative to enabling the ability to run PowerShell scripts and then disabling again once finished, the execution policy can be temporarily overridden whilst running a PowerShell script from the command line.

PowerShell.exe -ExecutionPolicy Bypass -File .\HelloWorld.ps1

Further Resources