Getting Started with PowerShell
Posted on Tue, Aug 23, 2011
At QuickStart and most companies, PowerShell is becoming more important to all Microsoft product administrators. Microsoft is making this the go-to command-line tool, and scripting tool for all its products. So, how do you get started? How hard is it to get started? 
First of all, PowerShell v2.0 is installed by default on all Windows 7 and Server 2008 R2 machines. Version 1.0 is installed on Vista machines and Server 2008. You should download version 2.0 and install it, since there are a number of features you get with version 2.0 that provide core functionality not included in version 1: the ability to run jobs in the background, the ability to manage remote machines and run your PowerShell commands on the remote server or servers using sessions, and the new debugging features. PowerShell v2.0 also has added an Integrated Scripting Environment (ISE), the new Microsoft GUI for running commands, and creating scripts. This is not installed by default. On Windows 7, you add this as a Windows component. In Server 2008, you add this in Server Manager as an additional Feature. So, now that you’ve installed it, how do you get started?
It’s not hard at all. There are only a few things you need to know to be able to run commands at the command line. All commands (called ‘commandlets’ and written cmdlets) have a verb-noun structure. So, to find all the cmdlets, you type: get-command. If you want commands that use a specific verb, like ‘get’ , you use the “-verb” parameter to specify which verbs to display. Or, alternatively, you can use the “-noun” switch to find cmdlets with a specific noun. So, “ Get-command -verb get”, will retrieve all the cmdlets that use get as a verb. If you want to find all the cmdlets that you can use to manage aliases, you can say: “Get-command -noun *alias*” . Note that you can use wildcards, which is handy if you aren’t sure what the noun might be. This last command will then show you the following cmdlets:
Export-alias, get-alias, import-alias, new-alias, and set-alias.
So, now that you’ve found the cmdlet you want to use, what are your syntax options? For this, you can use the “get-help” cmdlet. “Get-help get-alias”. This will give you the basic help file, without detailed information on parameters, and without examples. The help file that is displayed suggests (at the bottom) that you can also use one of the following parameters: “-full”, “-detailed”, or “-examples”. I particularly like the examples parameter, since I may know what the command is, but not be able to remember the common usage.
Now, how can you manipulate the output? You have used “get-process” to see all the processes, but you don’t like the output. Before I introduce you to the formatting options, you need to understand that PowerShell cmdlets emit objects, that can be passed on to the next cmdlet through something called a pipeline. Each cmdlet can pass the objects on, after performing its task, whether that is sorting, or filtering, or getting related information. The symbol for the pipeline is the vertical bar: |. So, if you want to change the default output from get-process, you can pass it to a formatting cmdlet.
You can choose your output with the “format” verb. To do this, you send the output from “get-process” through the pipeline to another cmdlet that specifies the format. You already found format-list by using: “get-command -verb *format*. (If you didn’t guess that the verb was format, you could always just use get-command, which will display all cmdlets.)
You find that your choices are format-table, format-list, format-wide, and format-custom. So you specify: “get-process | format-table”. This is the same output you got with just “get-process”. But if you send get-process to format-list, you get a shorter list of properties. To get all the properties you can use: “get-process | format-list *”. (Be sure you leave a space between list and the asterisk. ) Note that format-list is now being told to show all the properties, by using the asterisk.
Interested in learning about Powershell training and other QuickStart course? Please visit our course catalog or contact us today.
Please also share your SQL Server 2008 questions and comments below.
Also, to continue reading the rest of this PowerShell blog series, check out the seccond one: PowerShell Output- Tips and Traps.
Thank you!
Saskia Schott, Systems Engineer, QuickStart Intelligence