What’s new in Windows PowerShell 2.0

What’s new in Windows PowerShell 2.0

  • Comments 3
  • Likes

Hello AskPerf! Blake here again with another informative AskPerf Blog post. Today we are going to take a brief look at some of the new features in Windows PowerShell 2.0. As you may or may not know, PowerShell 2.0 is now included in Windows 7 and Windows Server 2008 R2. As of October 2009, PowerShell 2.0 is available to download for Windows XP SP3, Windows 2003 SP2, Windows Vista-SP1, and Windows Server 2008 RTM.

Windows PowerShell 2.0 includes several significant features that extend its use, improve its usability, and allow you to control and manage Windows-based environments more easily and comprehensively. Windows PowerShell 2.0 is backward compatible: Cmdlets, providers, snap-ins, scripts, functions, and profiles that were designed for Windows PowerShell 1.0 work in Windows PowerShell 2.0 without changes.

So with that, let’s check out some of the new features:

Remoting - The PowerShell 2.0 install now includes WinRm 2.0 (Windows Remote Management),  which is our implementation of the WS-Management Protocol. WS-Management allows systems the ability to access and exchange management information across an IT infrastructure. This new ability allows you to run commands on one or more remote computers from a single computer that is also running Windows PowerShell. To configure PowerShell remoting, the following must be accomplished:

1. Start PowerShell as an administrator

2. Use the “Enable-PSRemoting” cmdlet. The “Enable-PSRemoting” cmdlet performs the following operations:

    • Runs the Set-WSManQuickConfig cmdlet, which performs the following tasks:
    • Starts the WinRM service.
    • Sets the startup type on the WinRM service to Automatic.
    • Creates a listener to accept requests on any IP address.
    • Enables a firewall exception for WS-Management communications.
    • Enables all registered Windows PowerShell session configurations to receive instructions from a remote computer.
    • Registers the "Microsoft.PowerShell" session configuration, if it is not already registered.
    • Registers the "Microsoft.PowerShell32" session configuration on 64-bit computers, if it is not already registered.
    • Removes the "Deny Everyone" setting from the security descriptor for all the registered session configurations.
    • Restarts the WinRM service to make the preceding changes effective.

New Cmdlets - PowerShell 2.0 introduces over 100 built-in cmdlets. These cmdlets allow you to do computer-related, event log, and performance counter management tasks, among others. To see a list of new cmdlets that support the remote –computername parameter, type in the following command at a PS > prompt:

Get-Command -CommandType cmdlet | Where-Object { $_.definition -match 'computername' } | Format-Wide -property name –AutoSize

To view a list of all included cmdlets, run the following command at the PS> prompt:

get-commands

Script Debugging - For all you debuggers out there, you can now set breakpoints on lines, columns, variables and commands, and then let you specify the action that occurs when the breakpoint is hit. To get help on Debugging your PowerShell scripts, type in the following command at a PS > prompt:

Help Set-PSDebug

PowerShell GUI - Looking for a PowerShell GUI? Well, now you have it with the PowerShell Integrated Scripting Environment (ISE). This new functionality enables you to run interactive commands and edit and debug scripts in a graphical environment. The main features include color-coded syntax, selective execution, graphical debugging, Unicode support, and context-sensitive help. To launch the PowerShell GUI, run PowerShell_ISE.exe.

Note: you must install the Windows PowerShell Integrated Scripting Environment feature to get the PowerShell GUI.

Background Jobs – PowerShell 2.0 now allows you to run commands or expressions asynchronously and “in the background” without interacting with the console. When you run a background job, the command prompt returns immediately, even if the command is still running. The get-command *-job command will give you a list of job cmdlets that you can use to schedule your jobs.

This is only a small listing of new features included in PowerShell 2.0. To get a complete list, checkout the links below or the about_Windows_PowerShell_2.0 help section included with PowerShell 2.0.

Sample Script – The following is an example PowerShell script (non-supported) that uses the Get-EventLog cmdlet to parse the event logs for pages printed on your Windows XP or 2003 Server:

PrintCount.PS1

#####################################################################################

##

##   .\PrintCount.ps1 1/1/2010 1/31/2010

##

##   Tested on WinXP/Windows 2003

##

##   PowerShell 2.0 download link can be installed from

##   http://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx

#####################################################################################

$HelpSyntax=@"

SYNTAX: .\PrintCount.ps1 startDateTime [EndDateTime]

SAMPLE: To get pages printed in January 2010:

.\PrintCount.ps1 1/1/2010 2/1/2010

Note: Time is 12:00:00 AM

"@

If ($args.count -EQ 0)

{

write-host $HelpSyntax

}

Else

{

$StartDate = Get-date $args[0]

If ($args.count -EQ 1)

{

$EndDate = Get-date

}

Else

{

$EndDate = Get-date $args[1]

}

Write-host ""

Write-Host "Processing Print Event ID 10. From "

Write-host "$StartDate to $EndDate"

Write-host ""

get-eventlog -log system -source Print -after $StartDate -before $EndDate | where {$_.eventID -eq 10} |

Foreach-object -begin {Write-Host """==Printer=="",""==PagesPrinted=="""; $Total = 0;} `

{

$msg=[string] $_.Message;

$PrinterStart = $msg.indexof("was printed on") + 15;

$PrinterEnd = $msg.indexof("via port") - 1 ;

$PrinterLen = $PrinterEnd - $PrinterStart;

$Printer = $msg.substring($PrinterStart,$PrinterLen);

#Write-Host "Start=$PrinterStart End=$PrinterEnd Len= $PrinterLen"

$PagesPrintedStart = $PrinterStart = $msg.indexof("pages printed:") + 15;

$PagesPrintedLen = $msg.length - $PagesPrintedStart;

$PagesPrinted = $msg.substring($PagesPrintedStart,$PagesPrintedLen);

Write-Host """$Printer"",""$PagesPrinted"""

#Write-Host "Start=$PagesPrintedStart End=$PrinterEnd Len=$PagesPrintedLen"

$Total = $Total + [int] $PagesPrinted

};

Write-Host `n " __Total Pages printed: $Total" `n;

}

 

Additional Resources

· Getting Started with Windows PowerShell (v1.0)

· PowerShell Team Blog

· PowerShell 2.0 download (KB968929)

 

Until next time, happy Scripting!

Blake Morrison

 

Share this post :


Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Thanks for talking about using PowerShell for performance monitoring and I hope you will continue to provide some PowerShell examples.

    I do have a couple of suggestions for your script (to take advantage of some of the great built in functionality in PowerShell).

    First:

    Parameters - rather than parsing arguments, you can define parameters and let the PowerShell engine do that parsing for you.

    Example:

    Param (

               [parameter(Mandatory=$true

               Position=0)]

               [DateTime]

               $StartDate,

       [parameter(Position=1)]

               [DateTime]

               $EndDate,

             )

    This will allow you to use positional or named parameters and assign those values to variables of those names.  

    This would make the start date mandatory and allow for an optional end date parameter.

    Second, rather than using the $helpmesg variable, I would use the comment based help and use that message as an example.

    And using the above parameter syntax, the help system can provide basic syntax information to the user without having to write anything additional.

    Get-Help about_Comment_Based_Help can provide more detailed information on the inline help system in V2.

    Thanks and keep up the great work!

  • FYI There is a typo in your post - it should be "Get-Command" Not "get-commands"

  • If Powershell 2.0 is included in Windows 7, then why on all of our sytsems is it located in a folder labeled "1.0"?