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.
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.
The built-in help information can be accessed from the command line, using the ‘Get-Help’ cmdlet.
After executing the above, the help information for the ‘Write-Host’ cmdlet will be displayed.
Running a PowerShell Script
The ability to run a PowerShell script is disabled by default. This can be enabled using the ‘Set-ExecutionPolicy’ cmdlet in a command line interface as follows.
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.
If the script resides in the current directory, then there is an alternative way to run it from the command line.
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.