Click here to Skip to main content
15,878,814 members
Articles / Programming Languages / PowerShell

PowerShell 4.0 - What's New?

Rate me:
Please Sign up or sign in to vote.
4.71/5 (8 votes)
23 Sep 2013GPL35 min read 59.3K   227   24   11
This article presents you the latest PowerShell technology like new or extended cmdlets, Desired State Configuration, Windows Management Framework 4.0, and more.

Introduction

In this article, we'll discover some great new features that simplify the life of an administrator or an operator. We'll be shown features like Desired State Configuration. New and extended cmdlets will be demonstrated. Details about PowerShell internals and PowerShell 4.0 internals can be found in my previous article.

Getting Started

Installing PowerShell 4.0 Preview

Image 1

PowerShell is part of the Windows Management Framework 4.0 (WMF) and can be downloaded here.

The attentive reader might have discovered that there is no download for Windows 8. Why?

Microsoft expects every Windows 8 user to upgrade to Windows 8.1. Windows 8.1 will include WMF 4.0.

Before WMF 4.0 Preview can be installed, .NET Framework 4.5 must be installed! If .NET Framework 4.5 is not installed, amazingly, WMF4 reports that it has already been installed, which of course is nonsense.

Please pay attention to the link above as the Management Framework 4.0 Preview is not compatible with some Microsoft Server applications including all versions of Exchange Server, SharePoint Server, and other applications:

  • System Center 2012 Configuration Manager (not including SP1)
  • System Center Virtual Machine Manager 2008 R2 (including SP1)
  • Microsoft Exchange Server 2013, Microsoft Exchange Server 2010, and Microsoft Exchange Server 2007
  • Microsoft SharePoint Server 2013 and Microsoft SharePoint Server 2010
  • Windows Small Business Server 2011 and Windows Small Business Server 2008

You really shouldn’t be installing a preview in a production environment anyway, but rather using it locally on your workstation or laptop for testing and familiarization.

PowerShell ISE 4.0

If you use the Microsoft scripting editor, the following might be of great interest for you...

Microsoft has upgraded the integrated scripting environment to support:

  • both Windows PowerShell Workflow debugging and remote script debugging.
  • IntelliSense for Windows PowerShell Desired State Configuration providers and configurations.

Image 2

What Has Been Updated?

Besides some bug fixes and the newly added feature DSC, Microsoft has updated the following components:

  • Windows PowerShell Web Access
  • Windows PowerShell Web Services (Management OData IIS Extension)
  • Windows PowerShell Workflow
  • Windows PowerShell Integrated Scripting Environment (ISE)
  • Windows Remote Management (WinRM)
  • Windows Management Instrumentation (WMI)

What's New?

Take notice of the changed date (i.e. 2013) in the copyright string:

Image 3

Microsoft has updated the version strings as always:

Image 4

What's new besides the version information?

Desired State Configuration is a great and the only new feature we'll look at in the next section.

Desired State Configuration

A particularly exciting development is "Desired State Configuration" (DSC) with configurations that work much like PowerShell functions. DSC is a new way of configuring local and remote machines. Instead of writing a lot of script code, you can use the new configuration keyword which works very similarly to a PowerShell function. These configuration features are called blocks", with which one can either add or remove registry keys, files and folders, local users and groups, as well as Windows features (for servers).

Overview

The actual work is done inside of it, using DSC resources called "group", "service", or "registry", for example. A full list of resources shipped with DSC can be found here.

Each resource is technically represented by a DSC provider you can go and visit:

C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSProviders

Image 5

Note: The DSC providers are nothing other than PowerShell modules under the hood.

The resources that are available are described below:

Archive Resource

The Archive resource gives you a mechanism to unpack archive (.zip) files at a specific path.

C#
Archive ArchiveExample
{ 
   Ensure ="Present" # You can also set Ensure to "Absent"
   Path="C:\Users\Public\Documents\Test.zip"
   Destination ="C:\Users\Public\Documents\ExtractionPath"
}

Registry Resource

The Registry resource gives you a mechanism to manage registry keys and values.

Registry RegistryExample
{
   Ensure ="Present" # You can also set Ensure to "Absent"
   Key ="HKEY_LOCAL_MACHINE\SOFTWARE\ExampleKey"
   ValueName="TestValue"
   ValueData="TestData"
}

Script Resource

The Script resource gives you a mechanism to run Windows PowerShell script blocks on target nodes. The TestScript block runs first. If it returns False, the SetScript block will run. The GetScript block will run when you invoke the Get-DscConfiguration cmdlet (more on that cmdlet on the flipside of this sheet). GetScript must return a hash table.

Script ScriptExample
{
   SetScript={
      $sw=New-ObjectSystem.IO.StreamWriter("C:\TempFolder\TestFile.txt")
      $sw.WriteLine("Some sample string")
      $sw.Close()
   }
   TestScript ={Test-Path"C:\TempFolder\TestFile.txt"}
   GetScript={ <# This must return a hash table #>}
}

Package Resource

The Package resource gives you a mechanism to install and manage packages, such as MSI and setup.exe packages, on a target node.

Package PackageExample
{
   Ensure ="Present"# You can also set Ensure to "Absent"
   Path ="$Env:SystemDrive\TestFolder\TestProject.msi"
   Name ="TestPackage"
   ProductId ="663A8209-89E0-4C48-898B-53D73CA2C14B"
}

Environment Resource

The Environment resource gives you a mechanism to manage system environment variables.

Environment EnvironmentExample
{
   Ensure ="Present" # You can also set Ensure to "Absent"
   Name ="TestEnvironmentVariable"
   Value ="TestValue"
}

Group Resource

The Group resource gives you a mechanism to manage local groups on the target node.

Group GroupExample
{
   # This will remove TestGroup,if present
   # To create a new group, set Ensure to "Present"
   Ensure ="Absent"
   GroupName="TestGroup"
}

User Resource

The User resource gives you a mechanism to manage local user accounts on the target node.

User UserExample
{
   Ensure ="Present"# To delete a user account, set Ensure to "Absent"
   UserName="SomeName"
   Password =$passwordCred# This needs to be a credential object
   Requires ="[Group]GroupExample"# Configures GroupExamplefirst
}

Service Resource

The Service resource gives you a mechanism to manage services on the target node.

Service ServiceExample
{
   Name ="TermService"
   StartupType="Manual"
}

Putting It All Together

Creating a Configuration File

Please find below sample configuration file.

PHP
Configuration MyTestConfig
{
    param ($MachineName)
    
    Node $MachineName
    {
        Group TestGroup
        {
            Ensure ="Present" 
            GroupName="TestGroup" 
        }
    
        Service WinUpdate
        {
            Name ="wuauserv" 
            StartupType="Automatic" 
        }
    
        Script ScriptExample
        {
            SetScript={
                    $sw=New-Object System.IO.StreamWriter("$env:temp\TestFile.txt")
                    $sw.WriteLine("Some sample string")
                    $sw.Close()
            }
    
            TestScript ={Test-Path "C:\Temp\TestFile.txt"}
            GetScript={ <# This must return a hash table #> }
         }
    
         Registry RegistryExample
         {
                Ensure ="Present" # You can also set Ensure to "Absent" 
                Key ="HKEY_LOCAL_MACHINE\SOFTWARE\ExampleKey" 
                ValueName="TestValue" 
                ValueData="TestData" 
         }  
 
         Environment EnvironmentExample
         {
             Ensure ="Present" # You can also set Ensure to "Absent" 
             Name ="TestEnvironmentVariable"
             Value ="TestValue" 
         } 
     } 
}

Turning a Configuration into a MOF File

The actual configuration is not done by your script. Instead, your script takes the configuration data (your parameters) and turns it into a MOF file. Here are two lines of code that illustrate how you’d turn the above configuration into a MOF file:

PHP
$null = md C:\TEMP\dscconfig -ErrorAction SilentlyContinue
MyTestConfig -MachineName $env:COMPUTERNAME -OutputPath C:\TEMP\dscconfig 

Image 6

The resulting configuration file will be stored as {target node name}.MOF, if you specify multiple machine names multiple MOF-Files will be created - each MOF-File represents one target node. Your MOF-File will be similar to the one listed below (in my case, the target node name is 'DEVENV'):

PHP
/*
@TargetNode='DEVENV'
@GeneratedBy=Administrator
@GenerationDate=07/29/2013 09:07:05
@GenerationHost=DEVENV
*/
 
instance of MSFT_GroupResource as $MSFT_GroupResource1ref
{
ResourceID = "[Group]TestGroup";
 Ensure = "Present";
 GroupName = "TestGroup";
 SourceInfo = "::6::9::Group";
 ModuleName = "MSFT_GroupResource";
 ModuleVersion = "1.0";
 Requires = {
};
 
};
 
instance of MSFT_ServiceResource as $MSFT_ServiceResource1ref
{
ResourceID = "[Service]WinUpdate";
 SourceInfo = "::11::9::Service";
 Name = "wuauserv";
 StartupType = "Automatic";
 ModuleName = "MSFT_ServiceResource";
 ModuleVersion = "1.0";
 Requires = {
};
 
};
 
instance of MSFT_ScriptResource as $MSFT_ScriptResource1ref
{
ResourceID = "[Script]ScriptExample";
 GetScript = " <# This must return a hash table #> ";
 TestScript = "Test-Path \"C:\\Temp\\TestFile.txt\"";
 SourceInfo = "::16::9::Script";
 SetScript = "\n                    $sw=New-Object System.IO.StreamWriter(
   \"$env:temp\\TestFile.txt\")\n                    $sw.WriteLine(
   \"Some sample string\")\n                    $sw.Close()
   \n            ";
 ModuleName = "MSFT_ScriptResource";
 ModuleVersion = "1.0";
 Requires = {
};
 
};
 
instance of MSFT_RegistryResource as $MSFT_RegistryResource1ref
{
ResourceID = "[Registry]RegistryExample";
 ValueName = "TestValue";
 Key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\ExampleKey";
 Ensure = "Present";
 SourceInfo = "::26::10::Registry";
 ModuleName = "MSFT_RegistryResource";
 ValueData = {
    "TestData"
};
 ModuleVersion = "1.0";
 Requires = {
};
 
};
 
instance of MSFT_EnvironmentResource as $MSFT_EnvironmentResource1ref
{
ResourceID = "[Environment]EnvironmentExample";
 Ensure = "Present";
 Value = "TestValue";
 SourceInfo = "::33::10::Environment";
 Name = "TestEnvironmentVariable";
 ModuleName = "MSFT_EnvironmentResource";
 ModuleVersion = "1.0";
 Requires = {
};
 
};
 
instance of OMI_ConfigurationDocument
{
 Version="1.0.0";
 Author="Administrator";
 GenerationDate="07/29/2013 09:07:05";
 GenerationHost="DEVENV";
};

Applying a MOF File

Calling Start-DscConfiguration will invoke the DSC engine on the target machine(s). The actual work is done by the DSC resource (i.e., the PowerShell module).

Parameters

Path represents the target directory where your MOF-files are located.

Wait causes the execution of the DSC resources to run in background, i.e., an interactive process.

Start-DscConfiguration -Wait -Path C:\TEMP\dscconfig

Image 7

Result

Every step configured in our PowerShell test configuration file has been executed against the target machine(s). For example, the registry key including value and data has been created as we desired:

Image 8

Links

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Oliver Kohl D.Sc.
Austria Austria
https://www.conteco.gmbh

Comments and Discussions

 
GeneralMy vote of 5 Pin
zerocool1823-Sep-13 23:47
zerocool1823-Sep-13 23:47 
AnswerRe: My vote of 5 Pin
Oliver Kohl24-Sep-13 1:04
Oliver Kohl24-Sep-13 1:04 
QuestionPowerShell 2.0 and Powershell Remoting with Powershell 4.0 Pin
kiquenet.com8-Aug-13 2:12
professionalkiquenet.com8-Aug-13 2:12 
It's great.

I have Windows XP client with Powershell 2.0.

I have Windows 7 client with Powershell 2.0.

Maybe, any clients with Windows 7 using Powershell 3.0.

I have servers Windows 2008 R2 using Powerhell 2.0.

I use PowerShell Remoting to connect XP and Win7 clients to Win2008 servers.


Now, If I install Powershell 4.0 in Windows 2008 R2 servers.


My XP or Win7 client, that using Powershell 2.0 (maybe PS 3.0), will can connect with Powershell Remoting to Win2008 Server ?
kiquenet.com

AnswerRe: PowerShell 2.0 and Powershell Remoting with Powershell 4.0 Pin
Oliver Kohl8-Aug-13 2:53
Oliver Kohl8-Aug-13 2:53 
GeneralRe: PowerShell 2.0 and Powershell Remoting with Powershell 4.0 Pin
kiquenet.com8-Aug-13 21:17
professionalkiquenet.com8-Aug-13 21:17 
GeneralMy vote of 5 Pin
Mohammad132131229-Jul-13 6:17
Mohammad132131229-Jul-13 6:17 
AnswerRe: My vote of 5 Pin
Oliver Kohl29-Jul-13 6:39
Oliver Kohl29-Jul-13 6:39 
GeneralMy vote of 5 Pin
Satish Ratnaparkhi29-Jul-13 0:57
Satish Ratnaparkhi29-Jul-13 0:57 
AnswerRe: My vote of 5 Pin
Oliver Kohl29-Jul-13 2:15
Oliver Kohl29-Jul-13 2:15 
GeneralMy vote of 5 Pin
Verena12329-Jul-13 0:31
Verena12329-Jul-13 0:31 
AnswerRe: My vote of 5 Pin
Oliver Kohl29-Jul-13 0:35
Oliver Kohl29-Jul-13 0:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.