Prof. Powershell

The Two Faces of PowerShell

Save typing and use PowerShell as the scripting language.

PowerShell has two faces, and you are likely to see both of them. The first face is the management console. PowerShell is designed to run as an interactive console. You type a command or pipelined expression and PowerShell executes it. Because PowerShell is object-based and leverages the pipeline, what once required complex VBScript or batch files can now be accomplished with a single line or two of PowerShell. More than likely, 80 percent or more of your PowerShell usage will be through the interactive console.

You may have also heard that PowerShell is a scripting language. Well it is, but not necessarily in the way you'd think of a scripting language like VBScript.

What is the difference between using the interactive console and scripting? None. A PowerShell script is a simple text file with a .ps1 extension. Any command you type at the console can be pasted into your script. Think of a PowerShell script as a type of batch file. This means you can test your command interactively, and when it works, you can copy and paste it into your script.

The main reason you would write a script for PowerShell is essentially to save typing. When PowerShell runs your script, it reads and executes line by line, starting at the beginning. If you've got a long and complex PowerShell expression, especially one you will be using a lot, writing a script saves a lot of typing and mistakes. Run the script and your command executes. You might also need a script for more complex tasks that require several steps. You could execute the steps manually, but using a script saves time.

PowerShell's scripting language, such as it is, really only consists of a handful of logic constructs for handling If statements and loops. You can use them in the management console, but you are more likely to use them in a script. We'll cover them in future columns.

So don't be put off learning PowerShell, thinking you need to learn a new scripting language. As you learn the cmdlets, the pipeline, operators and everything else in the shell, you are learning to script at the same time. Interactive console or script? Whichever gets the job done the fastest. It's all the same to PowerShell.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular