PowerShell DSC allows you to install software and ensures the applications stay installed.
Profile gravatar of Adam Bertram

Adam Bertram

Adam is a Microsoft Cloud and Datacenter Management Most Valuable Profressional (MVP) who specializes in Windows PowerShell. You can reach Adam at adamtheautomator.com or on Twitter at @adbertram.
Profile gravatar of Adam Bertram

PowerShell Desired State Configuration (DSC) is a powerful, declarative language that allows administrators to guarantee a server stays the way you want. Whether you simply want to ensure certain registry keys exist, check that files are in a certain place or provision entire servers from scratch, virtually anything is possible with DSC.

DSC has a resources concept, which represents different sets of functionality. To install software onto a machine with DSC requires the Package resource. This allows you to define an installer package you'd like to install on a machine.

When DSC is invoked, this resource will first check to see if the software is already installed. If not, it will proceed to run the installer to get the software installed. Although the Package resource has some setbacks, it's still a good way to get "well behaved" packages installed on a number of machines.

For this article, I'm going to demonstrate installing the 7-Zip x64 MSI package with the Package resource, though you can also install EXE packages. You'll see that the 7-Zip MSI package works well with DSC, but not all MSI packages work equally as well. It depends on how the MSI was packaged.

First, you need to have PowerShell v4 or later installed. Otherwise, DSC is not available. You can always check this by typing $PSVersionTable.PSVersion.Major into the console. Once you've confirmed a current version, the next step is to ensure the Package resource is available by running Get-DscResource.

If it is available, you should see the Package resource properties.

Package resource properties

Package resource properties

I'll be installing 7-Zip on a single, local machine, so I'll need to check that the package exists on that machine. This is not a requirement, however, for the Package resource. I'll be using DSC in "push" mode here. If you've got lots of servers to install software on, look into setting up DSC in "pull" mode.

I've downloaded and placed the 7-Zip MSI package on my computer at C:\7z1514-x64.msi. Now that it's in place, I'll need to install the package once manually to gather a proper name and product ID for the configuration script. This is an important step, as these attributes detect if the software is installed.

Once installed, I'll note the name and productID it was installed with. To do this, I'll use the Get-InstalledSoftware function in my SoftwareInstallManager module. Otherwise, you'll need to scour the HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall and HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall registry keys looking for the 7-Zip key.

Retrieving the application name and product ID

Retrieving the application name and product ID

Now that I have the proper name and product ID, I can create the DSC configuration script.

You can see above that I have a user-defined configuration called Install7Zip and am pointing the install to the local computer name. Inside, I'm referencing the Package resource and calling it Install7Zip as well. I'd like to confirm the software is present on the local machine, and I've filled in the values we collected earlier.

Once the configuration is built, we'll need to execute it like a simple PowerShell function. This will create a MOF file with the name of the local computer in the location of the output path.

Creating the MOF file

Creating the MOF file

Once the MOF is generated, we just need to start the DSC configuration process by running Start-DscConfiguration-Path C:\Install7Zip-Wait-Verbose.

This will force DSC to first check that the software is installed. If not, it will run the MSI provided and install the software. By default, Start-DscConfiguration creates a job and runs in the background. Using the Wait parameter allows me to monitor the process as it goes along. You can see this process below.

Installing an application with PowerShell DSC

Installing an application with PowerShell DSC

The beauty of DSC is that you don't have to code any of the usual logic required to check if the software is installed or not. Simply start the DSC configuration again, and it knows the software is already set up. It won’t attempt to run the MSI again.

Checking if an applications is installed

Checking if an applications is installed

Installing software using the DSC Package resource can be advantageous for "well-behaved" EXE and MSI installers. However, as you attempt to deploy more software installers with it, you'll see that, due to the lack of standardization across many installers, it might not work as expected. If so, look into technologies like Choclatey or use PowerShell modules like my SoftwareInstallManager module to ease software deployments.

Take part in our competition and win $100!


Related Posts

  1. avatar
    Keith Sorbo 1 year ago

    What package / where did you get the get-InstalledSoftware script?

  2. avatar
    Victor Basic 4 weeks ago

    How can I reference the exe from an Azure Storage Account?

    • Profile gravatar of Adam Bertram Author
      Adam Bertram 3 weeks ago

      I'm not for sure. You'd have to get it downloaded locally first.

  3. avatar
    sand margin 3 weeks ago

    where did you get the get-InstalledSoftware script?


Leave a reply

Your email address will not be published. Required fields are marked *



Please ask IT administration questions in the forum. Any other messages are welcome.

© 4sysops 2006 - 2017

Log in with your credentials


Forgot your details?

Create Account