Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Export installed Chocolatey packages as packages.config - thanks to Matty666
#Put this in Export-Chocolatey.ps1 file and run it:
#.\Export-Chocolatey.ps1 > packages.config
#You can install the packages using
#choco install packages.config -y
Write-Output "<?xml version=`"1.0`" encoding=`"utf-8`"?>"
Write-Output "<packages>"
choco list -lo -r -y | % { " <package id=`"$($_.SubString(0, $_.IndexOf("|")))`" version=`"$($_.SubString($_.IndexOf("|") + 1))`" />" }
Write-Output "</packages>"
@wolfmagic7

This comment has been minimized.

Copy link

@wolfmagic7 wolfmagic7 commented Feb 2, 2017

this would be the command line to run on my old laptop that i then import into new laptop (i presume). can you tell me where i would find the resulting script and what it would be called? would it be easy enough to edit out packages i dont want on the new machine or would it be easier to do that in the gui version? thanks.

@rowellx68

This comment has been minimized.

Copy link

@rowellx68 rowellx68 commented Feb 8, 2017

Here's a version that specifically installs the version

choco list -lo -r -y | % { "choco install " + $_.Replace("|", " -version ") + " -y" }

@wolfmagic7 You can just pipe it to a file, something like this:

choco list -lo -r -y | % { "choco install " + $_.Replace("|", " -version ") + " -y" } > Install.ps1

And you should be in the directory where you ran the script.

@claudiuconstantin

This comment has been minimized.

Copy link

@claudiuconstantin claudiuconstantin commented Mar 30, 2017

'%' is not recognized as an internal or external command, operable program or batch file
Any idea?

@donbing

This comment has been minimized.

Copy link

@donbing donbing commented Apr 4, 2017

use power shell

@Matty666

This comment has been minimized.

Copy link

@Matty666 Matty666 commented Jul 5, 2017

I've changed this to write the output as a package.config file rather than individual choco install commands.

Write-Output "<?xml version=`"1.0`" encoding=`"utf-8`"?>"
Write-Output "<packages>"
choco list -lo -r -y | % { "   <package id=`"$($_.SubString(0, $_.IndexOf("|")))`" version=`"$($_.SubString($_.IndexOf("|") + 1))`" />" }
Write-Output "</packages>"

Put this in export.ps1 file and run it:

export.ps1 > packages.config

You can install the packages using

choco install packages.config -y

I prefer this because chocolatey will tell you which packages were successful etc. When running the original install.ps1 and some packages fail you don't really see them as it seems to just continue

@farmerbean

This comment has been minimized.

Copy link

@farmerbean farmerbean commented Jul 17, 2017

Nice @Matty666 - I dropped your script into a file, then put a wrapper round it so I can pump the whole thing to my onedrive folder and run a scheduled daily task to back it all up!

.\choco_backup.ps1 | out-file backup.config
$filenameFormat = (Get-Date -Format "yyyy-MM-dd") + "_"+ "backup.config"
Rename-Item -Path "backup.config" -NewName $filenameFormat
Return

Then run a Windows scheduled task (daily, in my case):

program/script: "c:\windows\system32\WindowsPowershell\v1.0\powershell.exe"
Add arguements: "-ExecutionPolicy Bypass C:\Users\badhatharry\OneDrive\System\Choco\choco_wrapper.ps1"
Start in: "C:\Users\badhatharry\OneDrive\System\Choco\"
@bcurran3

This comment has been minimized.

Copy link

@bcurran3 bcurran3 commented Aug 9, 2017

Hey guys!

@itopiacloud pointed me in this direction awhile ago when I was contemplating creating a script to backup my Chocolatey packages list.

I read this thread but the methods here just didn't go as far as I wanted, so I made a backup script as well. Not one for re-inventing the wheel, I couldn't have done it without @alimbada and @rowellx68's parsing. (I suck at parsing.) So thank you guys!

So here's me efforts, getting by a with a little help from my friends:
https://chocolatey.org/packages/choco-package-list-backup

@alimbada

This comment has been minimized.

Copy link
Owner Author

@alimbada alimbada commented Nov 29, 2017

I've updated the script with @Matty666's suggestions.

Apologies for the tardy response here; GitHub doesn't send notifications for comments on gists.

@mustafakibar

This comment has been minimized.

Copy link

@mustafakibar mustafakibar commented Jan 29, 2019

Here's a version that specifically installs the version

choco list -lo -r -y | % { "choco install " + $_.Replace("|", " -version ") + " -y" }

@wolfmagic7 You can just pipe it to a file, something like this:

choco list -lo -r -y | % { "choco install " + $_.Replace("|", " -version ") + " -y" } > Install.ps1

And you should be in the directory where you ran the script.

Without version information:

choco list -lo -r -y | % { "choco install " + $_.split('|')[0] + " -y" } > Install.ps1

@valentijnscholten

This comment has been minimized.

Copy link

@valentijnscholten valentijnscholten commented Apr 29, 2019

Thanks for the script. I kept running into a deserialization error on the xml header when installing on the new machine.

<?xml encoding="utf-8"?>

After trying all kinds of stuff, different encoding, including version, excluding encoding etc. etc. Still couldn't user the packages.config to install the packages. Turns out you can just remove the whole xml header and everything install fines :)

So just

<packages>
...
</packages>
@gricn

This comment has been minimized.

Copy link

@gricn gricn commented Dec 15, 2019

Thanks for the script. It would be better to explain some details in comment cause there are some mistakes confused me (cs freshman) for a while.
There are two ways to run the script.
Way 1 : cd to the folder where you create the ps1 file first. And run .\Export-Chocolatey.ps1 > packages.config instead of Export-Chocolatey.ps1 > packages.config
Way 2 : type the file with specific location instead, such as ~/desktop/Export-Chocolatey.ps1 > packages.config.

@alimbada

This comment has been minimized.

Copy link
Owner Author

@alimbada alimbada commented Jan 2, 2020

Thanks for the script. It would be better to explain some details in comment cause there are some mistakes confused me (cs freshman) for a while.
There are two ways to run the script.
Way 1 : cd to the folder where you create the ps1 file first. And run .\Export-Chocolatey.ps1 > packages.config instead of Export-Chocolatey.ps1 > packages.config
Way 2 : type the file with specific location instead, such as ~/desktop/Export-Chocolatey.ps1 > packages.config.

Thanks for the feedback. I've updated the comments in the script.

@WebTiger89

This comment has been minimized.

Copy link

@WebTiger89 WebTiger89 commented Feb 7, 2020

Is it possible to export the args/switches used on installation? Or alternatively does choco log the installation commands so I can catch them anywhere? For example when I write

cinstall notepadplusplus --install-args="'/DIR=C:\notepad'"

@infogulch

This comment has been minimized.

Copy link

@infogulch infogulch commented Mar 10, 2020

@WebTiger89 and others may be interested to see the documentation for packages.config:

https://chocolatey.org/docs/commandsinstall#packagesconfig

@MelbourneDeveloper

This comment has been minimized.

Copy link

@MelbourneDeveloper MelbourneDeveloper commented Oct 22, 2020

Just a comment about this feature, and also the ability to install from a package list...

We need the option to omit versions. We will probably want to install the same stuff but with the most recent version.

@infogulch

This comment has been minimized.

Copy link

@infogulch infogulch commented Nov 9, 2020

I created my own take on this kind of system in my own fork. https://gist.github.com/infogulch/0d81c3d4caccf2f1b6a82dc6f6459397

I based the workflow around committing updates to packages.config in git. See the README for details. I even ran the update script for my computer and committed it to demonstrate the intended workflow. You can find this commit in my gist's revisions. (Btw, if you clone your own gist to local you can commit and push back to the gist to update it.)

I disagree Melbourne, versions in packages.config mean you can know exactly what version is installed. Except if it's overwritten every time that information is lost. But if you keep it in a git repo then you get the best of both!

@battleunicorn

This comment has been minimized.

Copy link

@battleunicorn battleunicorn commented Aug 15, 2021

Write-Output "<?xml version=`"1.0`"encoding=`"utf-8`"?>"
Write-Output "<packages>"
choco list -lo -r -y |%{ "<package id=`"$($_.SubString(0,$_.IndexOf("|")))`"version=`"$($_.SubString($_.IndexOf("|") +1))`"/>"}
Write-Output "</packages>"
Out-File -FilePath .\packages.config

I added one line to this little script. Now, when it executes it automatically creates the pachages.config file that can be directly passed to chocolatey .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment