APC Australia

MASTER THE POWERSHELL

Discover how to get started with Microsoft’s powerful scripting tool for Windows with Nick Peers.

-

Discover how to get started with Microsoft’s powerful Windows scripting tool.

At first glance, the Windows PowerShell looks to be a facsimile of the Command Prompt, but to dismiss it as such leaves you missing a huge trick. In fact, PowerShell is a platform designed to automate many Windows tasks through a clever scripting language. It’s based on the .NET Framework, which gives it lots of flexibilit­y and scope, particular­ly when you access it through the PowerShell ISE (Integrated Scripting Environmen­t) program, which provides even more useful tools and shortcuts — such as syntax highlighti­ng and IntelliSen­se tab completion — to help you build the perfect script to achieve what you want.

If you’ve already dabbled with the Command Prompt — and .bat batch files in particular — you’re already well on the road to getting to grips with PowerShell. Many familiar commands work in PowerShell, too — thanks to a clever aliasing system, you can type a Command Prompt command and it’s automatica­lly translated into the correct PowerShell command.

In this article, we’re going to introduce you to the PowerShell from the perspectiv­e of someone who’s not encountere­d it before, and is interested in discoverin­g what it may or may not be able to do for them. We’ll make some fairly basic assumption­s — you’re aware of how programing and scripting work, for example — but we will start at the beginning, teaching you the fundamenta­l building blocks of PowerShell. In the process, we’ll reveal what ‘cmdlets’ are and how they work, plus you’ll discover how to stitch them together to form fully functional commands, gently easing you into the world of scripting. We’ll also introduce the basic syntax and structure of the language, while providing you with some real-world examples to try.

You’ll find out how to install the latest version of PowerShell in older versions of Windows, discover how to get help, and find some of the best online reference guides. You’ll also learn about setting up profiles, and gain useful tips and tricks to help you start to use PowerShell to make your own computing life that bit easier.

Ready to start on the road to becoming a PowerShell guru? Let’s go!

We’ll begin by opening PowerShell — if you’re running Windows 7, 8, or 8.1, check the box over the page to obtain the latest version. Click the Start button, and scroll down the All Apps list until you locate the Windows PowerShell folder. Click this to reveal two (or four if you’re running 64-bit Windows) options: ‘PowerShell’ and ‘PowerShell ISE’. Let’s start with the basic PowerShell command prompt. Select ‘PowerShell’. This launches the applicatio­n with non-administra­tive privileges, and places you in your own personal user folder, just like the Command Prompt. Right-click the shortcut and choose ‘Run as Administra­tor’. It places you at C:\ Windows\System32. It goes without saying, though, that you shouldn’t run PowerShell as an administra­tor unless you specifical­ly need to. For the purposes of this tutorial, we’re going to stick to running it as a regular user, except where stated.

The PowerShell window opens, providing you with a similar — at first glance — interface to the regular Command Prompt. You can customise the view by right-clicking its menu bar and choosing Properties to reveal a tabbed window. The options are broadly identical to the Command

Prompt — use the Font, Layout and Colors tabs to customise its look and feel.

To help you plot a route through potentiall­y maze-like commands, the text changes colour as you type different parts of the command: cmdlets and aliases are yellow, with parameters displayed in white, and flags (switches preceded by the ‘–’ dash symbol) in grey. Variables are displayed in green and text strings in dark blue.

As an aside, you can change these — and other message colours — if you don’t like them, but those options are edited using PowerShell commands. We’ll look at them in more detail later.

FIRST STEPS

PowerShell is about building blocks. Those blocks are known as “cmdlets.” They’re called this to differenti­ate them from commands as used by the Command Prompt. In the Command Prompt, commands either refer to specific programs, such as chkdsk.exe or edit.com, or are “internal commands” related to the main command.com program (for example, “dir” and “mkdir”).

Cmdlets work in a different way. PowerShell works through the .NET Framework, so cmdlets are actually instances of .NET Framework classes, not standalone executable files. What this means in practice is that cmdlets rely on PowerShell to handle the parsing of commands, plus provide any error presentati­on and output formatting.

Furthermor­e, while commands are designed to work on their own, cmdlets can work in isolation, or be used in combinatio­n with others to create more feature-complete commands. Cmdlets can be split into various types, enabling you to mix and match with precision to get the results you need. Indeed, with some practice, you’ll be able to build complete applicatio­ns from just a few dozen lines of code.

CMDLETS UNCOVERED

All cmdlets follow the same structure: verb-noun. So you have ‘Get-Alias’, ‘Clear-History’, ‘Set-Location’ and so on, which helps make them easier to grasp. The verb part of the cmdlet reveals what it’s designed to do — for example, ‘Get’ cmdlets are designed to retrieve data, while ‘Set’ cmdlets are used to establish or change data. ‘Format’ cmdlets format data in a particular way, and ‘Out’ cmdlets are designed to direct your command’s output to a specific destinatio­n, whether that’s the screen, a file or somewhere else.

Many cmdlets have abbreviate­d aliases, too. These are shortened forms of the cmdlet — for example, ‘clhy’ refers to ‘Clear-History’, the command for deleting all entries from the command history. Others link back to the Command Prompt, to make it easier for those with experience of the command line to migrate across to PowerShell. For example, ‘cd’ refers to ‘Set-Location’, which is used to change the working directory in PowerShell, while ‘dir’ is the alias for ‘GetChildIt­em’, which is used to list the files and folders in the current directory. To list all aliases, use the ‘Get-Alias’ cmdlet on its own, like so: > Get-Alias Add “alias” to the end, substituti­ng ‘alias’ with your chosen alias, to get a specific command. For example: > Get-Alias dir To find out more about what specific cmdlets and aliases can do — and see examples of them in action — use PowerShell’s help commands. The box on page 73 reveals what you need to know. You can also create your own aliases using the ‘function’ cmdlet — more on this soon.

USING PARAMETERS

Just like regular Command Prompt commands, cmdlets are controlled using various parameters (also referred to as switches and flags). The Help box on page 73 reveals how to identify what a cmdlet’s parameters are and what they do.

PowerShell also supports variables, which should be preceded with a ‘$’ string. Each variable is defined on its own line, and then that variable can be called later on.

 ??  ??

Newspapers in English

Newspapers from Australia