Running PowerShell In Vagrant

In this article, I will show how to configure a Vagrant box to run PowerShell.

One of key features of Vagrant is the ability to configure virtual machines by using not only traditional configuration management solutions such as Puppet, Ansible and Chef; but also with good old shell scripting. Since Vagrant was built with Linux in mind first, shell script was designed for bash. Fortunately, with the growing amount of Windows support in Hashicorps product suite, cmd and PowerShell are also supported for Windows boxes in Vagrant.

Not Just a Linux “Shell”

Whether you are executing bash, cmd or PowerShell on a Vagrant virtual machine, you will use the “shell” provisioner. When executing, there are two main methods, either by an inline command/script or by specifying the path to a script file relative to your Vagrant root folder.

There are also various options to add in Vagrant when executing shell. Vagrant offers two options that are specific to PowerShell. These are powershell_args and powershell_elevated_interactive. As you could guess, powershell_args means additional arguments to pass to powershell.exe. With the powershell_elevated_interactive option, this means that a PowerShell script will executive with privileges with the logged on user, in addition the “privileged” Vagrant option must also be set to “True”. Note that with the “privileged” field  set to “true”, the script runs as a schedule task on Windows.

PowerShell Demo

In this example, I want to write the script directly in my Vagrantfile, so I use the inline option to install the Chocolatey client on my Vagrant virtual machine during provisioning.

The output in Vagrant would appear something like this:


Leave a Reply

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