PowerShell 3.0 in Windows Server 2012

PowerShell v3 in Windows Server 2012Windows Server 2012 PowerShell 3.0

The purpose of this page is alert those who are already familiar with PowerShell to new developments for scripting in Server 2012.

 ♦

Introduction to Microsoft’s PowerShell

The benefits of using PowerShell 3 in Windows Server 2012 depends on whether you are coming from DOS or PowerShell 2. But no worries, I am assuming you have little or no previous exposure to PowerShell v 3.

History of PowerShell in Windows Server

  • Windows Server 2008 – PowerShell v1
  • Windows Server 2008 R2 – PowerShell v2 (Active Directory and Remoting)
  • Windows Server 2012 – PowerShell v3 (50+ Modules 250+ new cmdlets)

A key benefit of PowerShell is the ability to ‘fan out’ configuration instructions to multiple servers. As a result, you will not only more efficiently, but also get the pleasure of becoming a programmer

My example scripts are designed to get you started, they don’t really show-off PowerShell’s abilities as a scripting language. My message is that it’s easy to get started, yet a long journey to become a PowerShell expert.

New ‘Killer’ Features of PowerShell 3.0 in Server 2012

  • Active Directory – Sites
  • DNS and DHCP cmdlets (Brand new area for scripting)
  • Hyper-V via WMI
  • Networking (Tidied up 3 or 4 old WMI classes)
  • Printers
  • Storage

Installing PowerShell ISE on Windows Server 2012PowerShell 3 ISE in Windows 8 Server

Getting the Integrated Scripting Engine (GUI) version of PowerShell 3 is instructive on two counts. Firstly, as you scroll down through the Add Features for Windows PowerShell (ISE), check to see if there are any other features to select.

Secondly, you realize that the standard command-line version of Windows PowerShell is available by default the .NET4 Framework and PowerShell binaries are already installed 

Launching Windows PowerShell in Server 2012

  • Just as the front end of previous Microsoft servers, mimics the corresponding client, so Windows Server 2012 supplies the Metro UI as seen in Windows 7.PowerShell 3 in Windows 8 Server
  • After you logon, simply typing the letter ‘p’ will result in a list of all programs beginning with ‘p’.
  • I recommend pinning ‘Windows PowerShell ISE’ to the desktop. Do this right-click and select ‘Pin’ at the bottom right of the screen.
  • Pinning means that PowerShell will have a permanent tile on the Metro UI. I like to drag my PowerShell tile to the left of the screen so that it’s easier to find next time.

Check Your PowerShell’s Version

# PowerShell Ver
$Host
or better:
$PSVersionTable

Let us prove that Windows Server 2012 has version 3 of PowerShell. Assuming you have launched the ISE I type: $Host in the screen. However, others prefer the extra build information provided by $PSVersionTable.

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

New Cmdlets to View and Add Windows Features

Thanks to Add-WindowsFeature you can use PowerShell 3.0 to suppliment your Windows Server 2012 features. 

# PowerShell 3.0 adds Telnet to Windows Server 2012
Clear-Host
Add-WindowsFeature -Name Telnet

Further Research with Get-WindowsFeature

There are two reasons for using Get-WindowsFeature, to see what’s already installed, and as an imput for the Add- verb.

Clear-Host
Get-WindowsFeature | Where installed -eq $True

See more on Add-WindowsFeature

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

SolarWinds’ Network 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.

Perhaps the NPM’s best feature is the way it suggests solutions to network problems.  Its second best feature is 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 give this Network Performance Monitor a try.

Download your free trial of SolarWinds Network Performance Monitor.

PowerShell 3 Active Directory Examples

Firstly, check that Active Directory Module for Windows PowerShell has been installed. Here we employ PowerShell’s own Get-Command to list the Active Directory type of cmdlets that interest us:

# PowerShell Active Directory
Get-Command get-*ad*

Note 1:  We could also try: Get-Command -noun aduser.

Example: Get-User -filter -like

# PowerShell Active Directory Syntax
Get-AdUser -filter ‘name -like "Test*" ‘

Note 3:  Get-AdUser is nothing without a parameter. In this case we call for -filter, rather than -identity, -searchbase or -LDAPFilter parameter. Furthermore, pay close attention to the speech marks. For clarity it’s best to break-down into ‘name -like’ and the value, "Test*", then put it together as in the script above.

Other filter operators include -eq and -notLike. For certain LDAP properties -lt or -gt make sense. It is also possible to create a more complex query through the use of these two classic joiners: -and, -or.

Windows Server 2012 PowerShell TutorialMicrosoft Windows Server 2012 PowerShell 3

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 Server 2012 PowerShell Example
Get-Service

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 Server 2012 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 2:  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 Server 2012
Clear-Host
Get-EventLog application -newest 500 | where {$_.entryType -match "Error"}

These examples are just to whet your appetite, see more PowerShell tutorials

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

Troubleshooting PowerShell’s Get-AdUser

Firstly make sure that you are using Active Directory Module for Windows PowerShell. Check with Get-Module.

Secondly, to research Get-AdUser call for Get-Help and examine syntax and study the examples.

# PowerShell Active Directory Syntax
Clear-Host
Get-Help Get-AdUser -full

Note 3: I always append -full so that I can see Microsoft’s examples; sometimes I abreviate PowerShell’s Get-Help to plain: Help GetAdUser -full.

Exchange and Windows Server 2012 PowerShell

One of the uses of PowerShell is to run Exchange cmdlets and interrogate, 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 4: Naturally you need to be logged on with an account which has the appropriate Exchange Server roles.

See more ideas for Windows Server 2012 PowerShell scripts »

Summary Windows Server 2012 PowerShell

In Windows Server 2012, PowerShell is integrated into the operating system. This makes it easy to get started. When you need PowerShell it’s always worth getting the ISE version via the Add features wizard.

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

 


Microsoft Windows Server 2012 Topics

Windows Server 2012 Home   • Disable UAC Windows Server 2012   • Server 2012 Chkdsk Utility

Install Windows Server 2012   • PowerShell Add-WindowsFeature   • Network Performance Monitor

Shutdown Windows Server 2012   • Disable Shutdown Event Tracker Server 2012