Say Hello to PowerShell

PowerShell has been in development for over ten years. Microsoft continues to invest in its development and adoption. These investments have transformed the ease and robustness with which developers and admins can automate Windows tasks.


What is PowerShell?

PowerShell solves administration and adaptability challenges by seamlessly integrating the .NET Framework.

PowerShell is Microsoft's task automation framework, consisting of a command-line shell, an integrated scripting environment (ISE), a scripting language built on .NET Framework, an API allowing you to host PowerShell in your .NET applications, and it is a distributed automation platform. PowerShell provides full access to COM and WMI, enabling you to perform tasks on both local and remote Windows systems.

PowerShell is a new breed platform for automation, in that it solves administration and adaptability challenges by seamlessly integrating the .NET Framework. It's good for developers, administrators, testers and more. Based on .NET, the tool drives down costs, while providing developers and administrators a simple and enterprise-ready way to automate, measure and improve all of their processes.


Follow Along

In this intro, I'll highlight some of the depth and breadth PowerShell brings to the table. Things like the game changing object pipeline, the REPL (read, eval, print loop), .NET integration and more.

If you're running Windows 7 or 8, PowerShell is already installed. Download PowerShell v3 for Windows 7 here. You need Windows 7 SP1 and the download also works for Windows 2008 R2 SP1 and Windows 2008 SP2. If you're running Vista, XP or Windows 2003, you'll need to settle for PowerShell v2 here.


What is it Good For? Absolutely Everything!

From deploying multiple resources to Azure, to handling your software builds for continuous integration, accessing web services or REST endpoints. PowerShell has you covered and provides so much more.

  • Slice and dice text, XML, CSV, and JSON with ease
  • Embed PowerShell to provide scripting capabilities for your C# apps
  • Create GUI applications five to ten times faster with less code
  • Leverage PowerShell's capabilities to work with the Internet
  • Interact with DLLs and create objects, automatically display properties, and call methods in live interactive sessions
  • Build domain-specific languages (DSLs) and vocabularies to express solutions more clearly
  • Work with Microsoft Office via the Component Object Model (COM)
  • PowerShell v3 features

Bootstrapping PowerShell

What is a cmdlet? A cmdlet is the fundamental building block in PowerShell and is expressed as a verb-noun pair, helping to make them self-descriptive. For example:

PowerShell includes hundreds of core cmdlets and you can write your own.

Memorize Two Cmdlets

PowerShell makes discoverability easy. Whenever you walk up to the blue screen of PowerShell and are not sure what to do, use Get-Command.

The Get-Command cmdlet gets all commands that are installed on the computer, including cmdlets, aliases, functions, workflows, filters, scripts, and applications.

In essence, Get-Command shows you all the things you can do in the current PowerShell session.

Get-Command

Get-Command supports wildcards. Let's say I want to see all the cmdlets that have the verb, Invoke?

Here is what I get after pressing enter.

What are the cmdlets ending in the noun Item?

Note that you start to see other verbs, like Clear, New, Remove and Set.

Next I'll use a few cmdlets to summarize what is available in my session on one of my boxes when I launch the PowerShell console.

I call Get-Command, group it, and sort it. Here, I can see that I have 1000+ Cmdlets to work with. I'm running PowerShell v3 with additional modules installed, so your mileage may vary.

What modules do these cmdlets come from? We can answer that question this way:

There is a lot I can get accomplished with PowerShell.

Get-Help

Get-Help does exactly that, it displays help for the cmdlet you are what to know more about. Not only is it easy to get help, it is easy to create/include help for the cmdlets/advanced functions you develop, a topic for another article. Having help at your fingertips is a huge time saver.

Here is a Shortened version of the output.

Easier still; there is a switch for this on the Get-Help cmdlet that takes you here.

Want to just see examples how to use the cmdlet? Use the -Examples switch. Plus, you can copy and paste example directly into the console and run it.

It's that easy and quick.


PowerShell Easily Works with Web Services

The W3C defines a "Web service" as:

[...] a software system designed to support interoperable machine-to-machine interaction over a network. It has an interface described in a machine-processable format (specifically WSDL). Other systems interact with the Web service in a manner prescribed by its description using SOAP messages, typically conveyed using HTTP with an XML serialization in conjunction with other Web-related standards.

PowerShell makes quick work of Webservices, using the New-WebServiceProxy cmdlet. Change the $zipcode, run it and get the latest weather info.

Result

I store the proxy object in $svc and then call the GetCityForecastByZIP method, capturing the results in $result. Looping through the ForecastResult array, I transform the record on the data. Note that the City and DaytimeHigh are at different levels in the hierarchy.

The technique I am using is creating key/value pairs for the hashtable. @{} is PowerShell syntax for a new hashtable. Using [PSCustomObject] is a PowerShell accelerator, which allows me, for one thing, to create a custom object from a hashtable.

The Weather.asmx web service returned hierarchical XML. In a handful of PowerShell commands, I re-shaped the data and formatted it (the Date) to my liking. For this example, I chose to let PowerShell print it out. Once I have the data in my PowerShell session, I am now connected to the PowerShell ecosystem and can easily pipe this array of custom objects to other cmdlets to push it to another web service, generate HTML, save it to a file, create a CSV file or export it to a SQL database.

Working with CSV, JSON and XML

Slicing and dicing text formats is a PowerShell sweet spot. Here, I'll convert three common formats to a PowerShell object. For the CSV and JSON, I'll use the correct ConvertFrom-* cmdlets, and for the XML, I'll use the accelerator which takes XML and creates an XMLDocument.

So, we took three heterogeneous formats, and at the end, performed an aggregation over one of the fields. I could have retrieved each of these data feeds from various places, the CSV from a network share, the JSON for an REST query and the XML from a Web Service. Like I said, this is a PowerShell sweet spot.


Let's use Twitter's search REST API at the command line. I construct the Url, which you could use in your browser, and then I use the PowerShell cmdlet Invoke-RestMethod. It sends a request to the REST service and determines if the response is XML or JSON. Here I'm requesting a JSON response so, Invoke-RestMethod converts the results to an array of PowerShell objects. I pipe them to the Select cmdlet (an alias of the verb-noun Select-Object) choosing to only three fields. Think of this as a projection, similar to LINQ or SQL.

Result

It's that easy. Check out a video I did for version 2: "PowerShell, ShowUI and the Twitter API". A mini WPF Twitter app in handful of PowerShell.


Compiling C# on the Fly in PowerShell

PowerShell is an interpreted language. So, there will be a penalty. It is not a major concern, but, sometimes, you'll need the speed of a compiled language. PowerShell lets you compile C# on the fly in memory in your PowerShell session, by using the -TypeDefinition parameter on the Add-Type cmdlet. Note the here-string identified by the @" "@. They are super useful when quoting text. I encourage you to explore more with Add-Type and PowerShell's quoting rules.

Interacting with DLLS

Now, let's say I didn't give you the source code - just the compiled DLL. No problem: Add-Type has a -Path parameter.

Pay particular attention to this. Using Add-Type this way is how you easily wrap any .NET DLL coming from anybody, if they haven't already provided a PowerShell interface. This is true whether the DLL is from Microsoft, DELL, Citrix or another developer.


In Closing

So that does it for now. Would you like to see more Powershell-specific content on Nettuts+? Let us know below!

Tags:

Comments

Related Articles