Skip to content

Instantly share code, notes, and snippets.

@PatrickLang
Last active May 24, 2017 15:25
Show Gist options
  • Save PatrickLang/ffa83c778bbcc659ad86a0188f459946 to your computer and use it in GitHub Desktop.
Save PatrickLang/ffa83c778bbcc659ad86a0188f459946 to your computer and use it in GitHub Desktop.
PowerShell snippet for finding all Vagrantfiles under a path and showing the status

I've made the mistake of accidently removing VMs with Remove-VM or Hyper-V Manager that I originally created with Vagrant. This is a set of code snippets I'm using to help prevent that and make it easy to find all Vagrant created VMs, remove the ones I don't need, then delete the boxes.

$oldpwd = $pwd ; `
gci -r Vagrantfile | %{ `
  cd $_.Directory ; `
  Write-Output "Checking status at $pwd" ; `
  vagrant.exe status } ; `
cd $oldpwd

Example Output:

Checking status at D:\.vagrant.d\boxes\centos-6-1-1.x86_64\0\hyperv
Current machine states:

default                   not_created (hyperv)

Checking status at D:\.vagrant.d\boxes\centos-7-1-1.x86_64\0\hyperv
Current machine states:

default                   not_created (hyperv)

Checking status at D:\.vagrant.d\boxes\Dockercon-Demo\0\hyperv
Current machine states:

vagrant-windows-2016      not_created (hyperv)

Checking status at D:\.vagrant.d\boxes\symbols-VAGRANTSLASH-gentoo-docker\0.1.1\hyperv
Current machine states:

default                   not_created (hyperv)

Checking status at D:\.vagrant.d\boxes\windows_2016_docker\0\hyperv
Current machine states:

vagrant-windows-2016      not_created (hyperv)

Checking status at D:\Server1_v12
Current machine states:

default                   not_created (hyperv)

Checking status at D:\ubuntu1604
Current machine states:

default                   not_created (hyperv)

Checking status at D:\vms\centos
Current machine states:

default                   not_created (hyperv)

Other things I still need to do:

  • Purge unused boxes
    • Figure out how Vagrant refcounts things, if at all.
  • Write something to walk Packer's cache so I can clean up unneeded ISOs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment