PowerShell 3.0 in Windows 8

PowerShell 3.0 in Windows 8Windows 8 PowerShell 3.0

PowerShell is a wonderful command line equivalent of the Windows Explorer, taskmanager, or the DOS box.

Instead of drilling down through countless GUI menus you can type a few Verb-Noun commands and get the job done quicker and with more precision.

What’s new in PowerShell 3.0?  In a nutshell v 3.0 provides hundreds of extra cmdlets.  There is also a neat Auto-Complete prompt to help selecting commands.  However, the general improvements compared to v 2.0 are most useful for developers.

 ♦

Introduction to Microsoft’s PowerShell

The precice benefits of using PowerShell in Windows 8, rather than DOS or VBScript, depends on your background.  However, for the rest of this page, I am assuming you have little or no experience of PowerShell.  To newbies I would also like to say this, if you ever had the desire to be a computer programmer, then thanks to Microsoft’s PowerShell you will soon be writing scripts to interrogate the operating system.  As a result, you will not only be able to get data more efficiently, but also it will make tuning your computer configuration more pleasurable.

One by-product of PowerShell is that pure DOS commands are virtually redundant, even better, if you do need any of those old DOS commands, then they work in the PowerShell interface, thus you have nothing to lose by abandoning cmd.exe and trying PowerShell ISE with its graphical interface.

My examples underplay PowerShell’s abilities as a scripting language, this is because I don’t want to frighten people from learning PowerShell, just because they have no knowledge scripting languages such as C+ or VBScript.  My message is this: ‘it’s ridiculously easy to get started, but remember, it’s hard work to become truly expert in PowerShell’.

Getting Started with PowerShell in Windows 8PowerShell Scripts in Windows 8

Launching PowerShell in Microsoft’s Windows 8 is a trivial task; because unlike Vista, the .NET Framework and PowerShell binaries are already installed.

  • From the New UI, press the ‘p’ key.  You should see all the programs and Apps beginning with ‘p’.
  • I recommend right-clicking ‘Windows PowerShell ISE’, you should see ‘Pin’ at the bottom right of the sceen.
  • Pinning means that PowerShell will have a permenant tile on the Metro-style UI.  I like to drag my PowerShell tile to the left of the screen so that it’s easier to find next time.

Check PowerShell’s Version

# PowerShell Version
$Host

Let us begin by checking your version.  Assuming you have launched Microsoft’s PowerShell, simply type: $Host

Result
Name   : Windows PowerShell ISE Host 
Version : 3.0  [Assuming you have Windows 8]

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Windows 8 PowerShell TutorialMicrosoft Windows 8 PowerShell

The first point to recognise with PowerShell is the simplicity of the cmdlet.  At the heart of the script is always a Verb-Noun pairing.

Simple Examples:

# Window 8 PowerShell Example
Get-Process

or

Clear-Host
Get-Eventlog -list

For more ideas on nouns to pair with ‘Get’ try this:

Get-Command -verb get

Slightly more advanced Windows 8 PowerShell commands

The second point to note with PowerShell is the (|) Pipe symbol.  What this is does is enable the out put of the first cmdlet (Get-Service) to become the input of the second command (Where-Object)

Get-Service | Where-Object {$_.Status -eq "Stopped"}

Note:  As you become more experienced with PowerShell you can cut a few corners, for example Where-Object can be coded as plain Where.

Here is another PowerShell Script

# Simple PowerShell Script for Windows 8
Clear-Host
Get-EventLog application -newest 500 | where {$_.entryType -match "Error"}

These short examples are just to whet your appetite, see advanced Windows 8 PowerShell tutorials

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Orion performance monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

What’s New In Windows 8’s PowerShell 3.0?

The Auto-Complete feature is useful for checking commands.  As you begin to type a cmdlet you see a pick list of likely nouns to append to your verb.Microsoft Windows 8 PowerShell

Whereas PowerShell v 2.0 brought remoting, PowerShell v 3.0 brings new cmdlets.

# PowerShell 3.0 Cmdlet count
$Pv3 = Get-Command *
$Pv3.count

One example of the new cmdlets, PowerShell 3.0 employs AppX, which is a new method for deploying applications in Windows 8.  Lookout for Microsoft’s ‘Jupiter’ model, it’s designed along the same lines as Silverlight XAP.

See more new features in PowerShell 3.0

PowerShell Tutorial Based on Windows Files

Even experienced PowerShell users call for Get-Help when they need to research a new cmdlet.

# PowerShell Get-Help for Cmdlets
Clear-Host
Get-Help Get-ChildItem  -full

Note 2:  Actually, you don’t need ‘Get-‘, plain ‘Help Verb-Noun’ works because ‘Get’ is the default verb. Talking of assumptions PowerShell assumes that with Get-ChildItem the first parameter is for the location, thus you don’t need to explicitly use the -path parameter in most Get-ChildItem scripts.

Note 3:  One of the main uses of PowerShell’s Get-Help is for researching  -parameters for your task (Some people call them switches).  In this case -recurse, -include and possibly -force are important modifiers for the Get-ChildItem cmdlet.

Note 4:  Whenever I call for Get-Help I append the -full parameter because I always like to peruse the built-in examples.

PowerShell Tutorial: List Program .exe Files

Here is an example of a PowerShell script to mimic what you see in the Windows Explorer.  One benefit of this particular script is that it drills down through all the sub-directories under "Program Files\".

# PowerShell script to list the .exe files in the Program Files
Clear-Host
$Dir = Get-Childitem "C:\Program Files\" -recurse
# $Dir |Get-Member
$List = $Dir | where {$_.extension -eq ".exe"}
$List | format-Table name

Learning Points

Note 5: Beginning a script with Clear-Host is my way of clear the screen of any previous output (just as cls does in DOS).  The hash symbol # means a remark, or do not process this line.

Note 6:  $Dir = Get-Childitem C:\Program Files\ -recurse
This command sets the variable $Dir to the path of the files that we seek.  You have probably guessed the purpose of the -recurse switch, to drill down to the sub-folders.  Get-childitem is often abbreviated to its alias, gci.

Note 7:  $List = $Dir | where {$_.extension -eq ".exe"}
$List is another variable whose purpose is to filter the output, as a result we get only executables.  Pay close attention to the construction $_. which means, in this pipeline.  Also note that instead of the equals sign, PowerShell employs -eq.

Note 8:  PowerShell’s signature tune is the pipe symbol (|).  Most PowerShell scripts contain at least one pipe to filter the output of the first command so that it becomes the input of the second clause.

Note  Out-GridView: Is a useful cmdlet to control data display.  See more on how to pipe the results into out-GridView.

Exchange and Windows 8 PowerShell

One of the uses of PowerShell even on a Windows 8 laptop is to run Exchange cmdlets and interogate, or even configure, the Exchange Server; here are examples:

Home-in on one particular user:

Get-Mailbox -Identity "Guy Thomas"

Or get information about a particular Exchange mailstore database:

Get-Mailbox -Database Exch01

Note 9: Naturally you need to be logged on with an account which has the appropriate Exchange Server roles.

See more ideas for Windows 8 PowerShell scripts.

Exchange Monitor from SolarWindsGuy Recommends: The SolarWinds Exchange Monitor

Here is a free tool to monitor your Exchange Server.  Download and install the utility, then inspect your mail queues, monitor the Exchange server’s memory, confirm there is enough disk space and check the CPU utilization.

This is the real deal – there is no catch.  SolarWinds provides this fully-functioning freebie, as part of their commitment to supporting the network management community.

Free Download of SolarWinds Exchange Monitor

PowerShell Registry

As a beginner, people will tell you that accessing the registry with PowerShell is as easy as accessing the file system.  Guy says that doing useful work means learning knack.  Let start with PowerShell’s PSDrive provider, which opens the door to the registry.  Try this in PowerShell:

CD HKCU:\   (Just as easily as when you type: cd C:\)

See more on editing the PowerShell Registry

Summary of PowerShell 3.0 for Windows 8

The way to get started is to Pin the ISE version to the Metro UI.  To do this just type ‘p’ and select Windows PowerShell ISE from the list of Apps.

If you have experience of PowerShell then what’s new is more cmdlets, and more enticements for developers to use PowerShell to manage multiple servers.

If you like this page then please share it with your friends

 


Microsoft Windows 8 PowerShell Topics

Windows 8 Overview  • Windows 8 PowerShell  • Win 8 PowerShell Scripts   • Free WMI Monitor

Windows 8 Registry  • Windows 8 Registry Hacks  • Activate Administrator Windows 8

Win 8 Registry  • Windows 8 AutoRun  • Review WMI Monitor  • SolarWinds Mobile App Manager