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:
PS C:\> Get-Date Sunday, May 26, 2013 8:46:16 AM
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
?
Get-Command Invoke*
Here is what I get after pressing enter
.
CommandType Name ModuleName ----------- ---- ---------- Function Invoke-AsWorkflow PSWorkflowUtility Function Invoke-Background ShowUI Function Invoke-BrowserControl AutoBrowse Function Invoke-Line isepack Function Invoke-Line PowerShellPack Function Invoke-Office365 Pipeworks Function Invoke-Pester Pester Function Invoke-WebCommand Pipeworks Cmdlet Invoke-CimMethod CimCmdlets Cmdlet Invoke-Command Microsoft.PowerShell.Core Cmdlet Invoke-Expression Microsoft.PowerShell.Utility Cmdlet Invoke-History Microsoft.PowerShell.Core Cmdlet Invoke-Item Microsoft.PowerShell.Management Cmdlet Invoke-RestMethod Microsoft.PowerShell.Utility Cmdlet Invoke-TroubleshootingPack TroubleshootingPack Cmdlet Invoke-WebRequest Microsoft.PowerShell.Utility Cmdlet Invoke-WmiMethod Microsoft.PowerShell.Management Cmdlet Invoke-WSManAction Microsoft.WSMan.Management
What are the cmdlets ending in the noun Item?
Get-Command *-Item
Note that you start to see other verbs, like Clear, New, Remove and Set.
CommandType Name ModuleName ----------- ---- ---------- Cmdlet Clear-Item Microsoft.PowerShell.Management Cmdlet Copy-Item Microsoft.PowerShell.Management Cmdlet Get-Item Microsoft.PowerShell.Management Cmdlet Invoke-Item Microsoft.PowerShell.Management Cmdlet Move-Item Microsoft.PowerShell.Management Cmdlet New-Item Microsoft.PowerShell.Management Cmdlet Remove-Item Microsoft.PowerShell.Management Cmdlet Rename-Item Microsoft.PowerShell.Management Cmdlet Set-Item Microsoft.PowerShell.Management
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.
Get-Command | Group CommandType -NoElement | Sort Count -Descending
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.
Count Name ----- ---- 2487 Function 1184 Cmdlet 38 Alias 1 Filter
What modules do these cmdlets come from? We can answer that question this way:
Get-Command -CommandType Cmdlet | Group ModuleName -NoElement | Sort Count -Descending
There is a lot I can get accomplished with PowerShell.
Count Name ----- ---- 379 ShowUI 164 Hyper-V 157 Azure 92 Microsoft.PowerShell.Utility 82 Microsoft.PowerShell.Management 78 WebAdministration 55 Microsoft.PowerShell.Core 22 Dism 18 International 17 PKI 16 PSScheduledJob 13 Microsoft.WSMan.Management 12 CimCmdlets 10 Microsoft.PowerShell.Security 9 TrustedPlatformModule 8 BitsTransfer 8 MsDtc 6 Pipeworks 6 Kds 5 AppLocker 5 SecureBoot 5 Microsoft.PowerShell.Diagnostics 4 NetSecurity 4 Appx 3 WindowsErrorReporting 2 Microsoft.PowerShell.Host 2 TroubleshootingPack 1 PSWorkflow 1 DnsClient
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.
Get-Help Invoke-Command
Here is a Shortened version of the output.
NAME Get-Process SYNOPSIS Gets the processes that are running on the local computer or a remote computer. SYNTAX Get-Process [[-Name] <String[]>] [-ComputerName <String[]>] [-FileVersionInfo [<SwitchParameter>]] [-Module [<SwitchParameter>]] [<CommonParameters>] Get-Process [-ComputerName <String[]>] [-FileVersionInfo [<SwitchParameter>]] [-Module [<SwitchParameter>]] -Id <Int32[]> [<CommonParameters>] Get-Process [-ComputerName <String[]>] [-FileVersionInfo [<SwitchParameter>]] [-Module [<SwitchParameter>]] -InputObject <Process[]> [<CommonParameters>] DESCRIPTION The Get-Process cmdlet gets the processes on a local or remote computer. . . . .
Easier still; there is a switch for this on the Get-Help cmdlet that takes you here.
Get-Help Get-Process -Online
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.
Get-Help Get-Process -Examples
It's that easy and quick.
NAME Get-Process SYNOPSIS Gets the processes that are running on the local computer or a remote computer. -------------------------- EXAMPLE 1 -------------------------- PS C:\> Get-Process This command gets a list of all of the running processes running on the local computer. For a definition of each column, see the "Additional Notes" section of the Help topic for Get-Help. -------------------------- EXAMPLE 2 -------------------------- PS C:\> Get-Process winword, explorer | format-list *
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.
$zipCode = "96826" $svc = New-WebServiceProxy http://wsf.cdyne.com/WeatherWS/Weather.asmx $result = $svc.GetCityForecastByZIP($zipCode) # Transform the results $result.ForecastResult | ForEach { [PSCustomObject]@{ City = $result.City State = $result.State Date = $_.Date.ToString("d") Description = $_.Desciption DaytimeHigh = $_.Temperatures.DaytimeHigh } }
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.
City State Date Description DaytimeHigh ---- ----- ---- ----------- ----------- Honolulu HI 5/22/2013 Showers 76 Honolulu HI 5/23/2013 Partly Cloudy 76 Honolulu HI 5/24/2013 Partly Cloudy 77 Honolulu HI 5/25/2013 Partly Cloudy 77 Honolulu HI 5/26/2013 Partly Cloudy 77 Honolulu HI 5/27/2013 Partly Cloudy 77 Honolulu HI 5/28/2013 Partly Cloudy 77
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.
# Use CSV $csv = "Name,Age`r`nJohn,10" | ConvertFrom-Csv $csv Name Age ---- --- John 10 # Use JSON $json = "{Name:'Tom', Age:20}" | ConvertFrom-Json $json Name Age ---- --- Tom 20 # Use XML $xml = ([xml][/xml]"<data><record><Name>Harry</Name><Age>30</Age></record></data>").data.record $xml Name Age ---- --- Harry 30 # Combine all three $csv,$json,$xml Name Age ---- --- John 10 Tom 20 Harry 30 # Add up the ages $csv,$json,$xml | % {$sum=0} {$sum+=$_.age} {$sum} 60
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.
Twitter Search
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.
$query = "PowerShell" $url = "http://search.twitter.com/search.json?q=$query" (Invoke-RestMethod $url).results | Select created_at, from_user_name, text
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.
created_at from_user_name text ---------- -------------- ---- Sat, 25 May 2013 vitor pombeiro @brunomlopes a falar sobre Powershell "à minha maneira" Sat, 25 May 2013 Jeffery Hicks Did you know the #PowerShell ISE has startup options? In Sat, 25 May 2013 Pat Richard @mwjcomputing Yeah - had always used $MyInvocation.MyCom Sat, 25 May 2013 Rob Fairman "#PowerShell Script for Clearing #Windows Event Logs" ht Sat, 25 May 2013 Jim Priestley Automating SharePoint Deployments in Windows #Azure usin Sat, 25 May 2013 VT Technology RT @jangwoo_park: “@VTTechnology: Export Multiple Virtua Sat, 25 May 2013 Aryan Nava Using PowerShell to view Site created on the previous da Sat, 25 May 2013 Aryan Nava PowerShell Tips for SQL Server http://t.co/lVW2AY5BYZ Sat, 25 May 2013 Private Cloud Tech The Journey to the Private Cloud Part 3 – The Infrastruc Sat, 25 May 2013 Dr Tom Shinder The Journey to the Private Cloud Part 3 – The Infrastruc Sat, 25 May 2013 Jacob Daniels RT @tech_faq: Windows Server Manager is based on Windows Sat, 25 May 2013 CodeCruiser 10 Tips for the SQL Server PowerShell Scripter http://t.
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.
Add-Type -TypeDefinition @" public class TestInline { public long Fibonacci(long n) { long a = 0; long b = 1; for (long idx = 0; idx < n; idx+=1) { long temp = a; a = b; b = temp + b; } return a; } } "@ $obj = New-Object TestInline 1..10 | ForEach { $obj.Fibonacci($_) }
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.
Add-Type -Path .\FibLib.dll $obj = New-Object TestInline 1..10 | ForEach { $obj.Fibonacci($_) }
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!
Comments