Automatically Generating a Chocolatey Install Script

Have you ever dreaded formatting your computer because of all the software you know you’re going to have to re-install? Do you wish there was an easier way?

I’m looking at having to format one of the computers in our office and I want it have the same software that I have installed on my primary machine.

Since I use Chocolatey and WebPI to install most of the software on my computer, I was able to create a neat little Powershell script that should save me a lot of time with this task. 

Basically, this little bit of code generates a Chocolatey install script based on the following:

  • The current set of Windows features that you have enabled
  • Any software that you’ve installed through Windows Platform Installer (WebPI)
  • Any packages you’ve installed with Chocolatey.

It’s like taking a snapshot of parts of your software environment so you can re-install those parts later.

Here’s the script:

$windowsfeatures = choco list -source windowsfeatures

$windowsfeatures | ForEach-Object { 
   
    $line = "choco windowsfeatures "

    if ($_ -match "\| Enabled") {
        
        $line += $_ -replace " {0,1}\|.*", ""
        $line
    }
}

$webpi = choco list -source webpi
$gotAll = 0
$start = 0;

$webpi | ForEach-Object { 

    if ($_ -contains "--Available Products") {
        $gotAll = 1;
    }
            
    if ($_ -eq "----------------------------------------") {
        $start = 1;
    }

    $line = "choco webpi "

    if ($start -eq 1 -and $gotAll -eq 0 -and $_ -ne "" -and $_ -ne "    " -and $_ -ne "----------------------------------------") {
        
        $line += $_ -replace " .*", ""
        $line
    }
}

$local = choco list -localonly

$local | ForEach-Object { 


    $line = "choco install "
    $line += $_ -replace " .*", ""
    $line
}

Running the script in Powershell spits out the following:

choco windowsfeatures Microsoft-Hyper-V-All                                
choco windowsfeatures Microsoft-Hyper-V-Tools-All                          
choco windowsfeatures Microsoft-Hyper-V                                    
choco windowsfeatures Microsoft-Hyper-V-Management-Clients                 
choco windowsfeatures Microsoft-Hyper-V-Management-PowerShell              
choco windowsfeatures Printing-Foundation-Features                         
choco windowsfeatures Printing-Foundation-InternetPrinting-Client          
choco windowsfeatures FaxServicesClientPackage                             
choco windowsfeatures ScanManagementConsole                                
choco windowsfeatures Windows-Defender-Default-Definitions                 
choco windowsfeatures Windows-Identity-Foundation                          
choco windowsfeatures MicrosoftWindowsPowerShellV2Root                     
choco windowsfeatures MicrosoftWindowsPowerShellV2                         
choco windowsfeatures Internet-Explorer-Optional-amd64                     
choco windowsfeatures NetFx3                                               
choco windowsfeatures IIS-WebServerRole                                    
choco windowsfeatures IIS-WebServer                                        
choco windowsfeatures IIS-CommonHttpFeatures                               
choco windowsfeatures IIS-HttpErrors                                       
choco windowsfeatures IIS-HttpRedirect                                     
choco windowsfeatures IIS-ApplicationDevelopment                           
choco windowsfeatures IIS-NetFxExtensibility                               
choco windowsfeatures IIS-NetFxExtensibility45                             
choco windowsfeatures IIS-HealthAndDiagnostics                             
choco windowsfeatures IIS-HttpLogging                                      
choco windowsfeatures IIS-LoggingLibraries                                 
choco windowsfeatures IIS-RequestMonitor                                   
choco windowsfeatures IIS-HttpTracing                                      
choco windowsfeatures IIS-Security                                         
choco windowsfeatures IIS-URLAuthorization                                 
choco windowsfeatures IIS-RequestFiltering                                 
choco windowsfeatures IIS-IPSecurity                                       
choco windowsfeatures IIS-Performance                                      
choco windowsfeatures IIS-HttpCompressionDynamic                           
choco windowsfeatures IIS-WebServerManagementTools                         
choco windowsfeatures IIS-ManagementScriptingTools                         
choco windowsfeatures IIS-IIS6ManagementCompatibility                      
choco windowsfeatures IIS-Metabase                                         
choco windowsfeatures WAS-WindowsActivationService                         
choco windowsfeatures WAS-ProcessModel                                     
choco windowsfeatures WAS-NetFxEnvironment                                 
choco windowsfeatures WAS-ConfigurationAPI                                 
choco windowsfeatures IIS-CertProvider                                     
choco windowsfeatures IIS-WindowsAuthentication                            
choco windowsfeatures IIS-DigestAuthentication                             
choco windowsfeatures IIS-ClientCertificateMappingAuthentication           
choco windowsfeatures IIS-IISCertificateMappingAuthentication              
choco windowsfeatures IIS-ODBCLogging                                      
choco windowsfeatures IIS-StaticContent                                    
choco windowsfeatures IIS-DefaultDocument                                  
choco windowsfeatures IIS-DirectoryBrowsing                                
choco windowsfeatures IIS-WebDAV                                           
choco windowsfeatures IIS-WebSockets                                       
choco windowsfeatures IIS-ApplicationInit                                  
choco windowsfeatures IIS-ASPNET                                           
choco windowsfeatures IIS-ASPNET45                                         
choco windowsfeatures IIS-ASP                                              
choco windowsfeatures IIS-CGI                                              
choco windowsfeatures IIS-ISAPIExtensions                                  
choco windowsfeatures IIS-ISAPIFilter                                      
choco windowsfeatures IIS-ServerSideIncludes                               
choco windowsfeatures IIS-CustomLogging                                    
choco windowsfeatures IIS-BasicAuthentication                              
choco windowsfeatures IIS-HttpCompressionStatic                            
choco windowsfeatures IIS-ManagementConsole                                
choco windowsfeatures IIS-ManagementService                                
choco windowsfeatures WCF-Services45                                       
choco windowsfeatures WCF-HTTP-Activation45                                
choco windowsfeatures WCF-TCP-Activation45                                 
choco windowsfeatures WCF-Pipe-Activation45                                
choco windowsfeatures WCF-TCP-PortSharing45                                
choco windowsfeatures WCF-HTTP-Activation                                  
choco windowsfeatures WCF-NonHTTP-Activation                               
choco windowsfeatures NetFx4-AdvSrvs                                       
choco windowsfeatures NetFx4Extended-ASPNET45                              
choco windowsfeatures MediaPlayback                                        
choco windowsfeatures WindowsMediaPlayer                                   
choco windowsfeatures Microsoft-Windows-MobilePC-LocationProvider-INF      
choco windowsfeatures Printing-XPSServices-Features                        
choco windowsfeatures MSRDC-Infrastructure                                 
choco windowsfeatures SearchEngine-Client-Package                          
choco windowsfeatures TelnetClient                                         
choco windowsfeatures TFTP                                                 
choco windowsfeatures Xps-Foundation-Xps-Viewer                            
choco windowsfeatures WorkFolders-Client                                   
choco windowsfeatures SMB1Protocol                                         
choco webpi AppWarmUp
choco webpi ASP
choco webpi ASPNET
choco webpi ASPNET45
choco webpi ASPNET_REGIIS_NET4
choco webpi AzureQuickStarts_1_5_0
choco webpi BasicAuthentication
choco webpi CertProvider
choco webpi CGI
choco webpi ClientCertificateMappingAuthentication
choco webpi CustomLogging
choco webpi DefaultDocument
choco webpi DigestAuthentication
choco webpi DirectoryBrowse
choco webpi DynamicContentCompression
choco webpi HiveODBCDriver_x64_2_5
choco webpi HiveODBCDriver_x86_2_5
choco webpi HTTPErrors
choco webpi HTTPLogging
choco webpi HTTPRedirection
choco webpi IIS7
choco webpi IISClientCertificateMappingAuthentication
choco webpi IISExpress
choco webpi IISExpress_7_5
choco webpi IISExpress_ByPlatform
choco webpi IISManagementConsole
choco webpi IISManagementScriptsAndTools
choco webpi IPSecurity
choco webpi ISAPIExtensions
choco webpi ISAPIFilters
choco webpi LoggingTools
choco webpi MetabaseAndIIS6Compatibility
choco webpi NETExtensibility
choco webpi NETFramework35
choco webpi NETFramework4Update402
choco webpi NetFx3
choco webpi NetFx4
choco webpi NetFx4Extended-ASPNET45
choco webpi NetFxExtensibility45
choco webpi ODBCLogging
choco webpi PowerShell
choco webpi PowerShell3
choco webpi PowerShell4
choco webpi RequestFiltering
choco webpi RequestMonitor
choco webpi ServerSideIncludes
choco webpi SMO
choco webpi SMO_11_0
choco webpi SQLCE
choco webpi SQLCEforWM
choco webpi SQLCE_4_0
choco webpi SQLCLRTypes
choco webpi SQLDOM
choco webpi SQLLocalDB
choco webpi StaticContent
choco webpi StaticContentCompression
choco webpi Tracing
choco webpi URLAuthorization
choco webpi VS2013CommunityAzurePack
choco webpi VS2013CommunityAzurePack.2.5
choco webpi VWDOrVs2013AzurePack
choco webpi VWDOrVs2013AzurePack.2.4
choco webpi VWDOrVs2013AzurePack.2.5
choco webpi WASConfigurationAPI
choco webpi WASNetFxEnvironment
choco webpi WASProcessModel
choco webpi WDeploy
choco webpi WDeployNoSMO
choco webpi WebDAV
choco webpi WebSockets
choco webpi WIF
choco webpi WindowsAuthentication
choco webpi WindowsAzureSDK_2_4_1
choco webpi WindowsAzureSDK_2_5
choco webpi WindowsAzureSDK_2_5.VS2013
choco webpi WindowsAzureSDK_2_5.VS2015
choco webpi WindowsInstaller31
choco webpi WindowsInstaller45
choco webpi WorkflowClient
choco webpi WorkflowManagerToolsVS2012
choco install 7zip.install
choco install ctags
choco install curl
choco install filezilla
choco install git.install
choco install javaruntime
choco install KickAssVim
choco install linqpad4
choco install linqpad4.install
choco install nircmd
choco install nodejs.install
choco install SublimeText3
choco install sysinternals
choco install vim
choco install vlc
choco install warmup
choco install windirstat
choco install wixtoolset

Save that off somewhere and use it as a starting point the next time you have to format your computer!

It’s probably a good idea to schedule the Powershell script to run periodically, and back it up, so you’ll always have a current version of your chocolatey install script.

The key, of course, is to always use Chocolatey and Web Platform Installer to install all of the software on your computer. But even if it’s not possible to install every piece of software with Chocolatey and WebPI, you can see from my install script above that it’s still possible to save a LOT of time with this technique.

Published by alexdresko

To learn more about me, check the "About Me" page on this site.

Scroll Up