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.

test.vm.provision "shell", inline: <<-SHELL

       Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString(''))

       Set-TimeZone 'Eastern Standard Time' 


The output in Vagrant would appear something like this:

est: Running: inline PowerShell script

    test: Getting latest version of the Chocolatey package for download.

    test: Getting Chocolatey from

    test: Downloading 7-Zip commandline tool prior to extraction.

    test: Extracting C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocInstall\ to C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocInstall...

    test: Installing chocolatey on this machine

    test: Creating ChocolateyInstall as an environment variable (targeting 'Machine') 

    test:   Setting ChocolateyInstall to 'C:\ProgramData\chocolatey'

    test: WARNING: It's very likely you will need to close and reopen your shell 

    test:   before you can use choco.

    test: Restricting write permissions to Administrators

    test: We are setting up the Chocolatey package repository.

    test: The packages themselves go to 'C:\ProgramData\chocolatey\lib'

    test:   (i.e. C:\ProgramData\chocolatey\lib\yourPackageName).

    test: A shim file for the command line goes to 'C:\ProgramData\chocolatey\bin'

    test:   and points to an executable in 'C:\ProgramData\chocolatey\lib\yourPackageName'.

    test: Creating Chocolatey folders if they do not already exist.

    test: WARNING: You can safely ignore errors related to missing log files when 

    test:   upgrading from a version of Chocolatey less than 0.9.9. 

    test:   'Batch file could not be found' is also safe to ignore. 

    test:   'The system cannot find the file specified' - also safe.

    test: chocolatey.nupkg file not installed in lib.

    test:  Attempting to locate it from bootstrapper.

    test: PATH environment variable does not have C:\ProgramData\chocolatey\bin in it. Adding...

    test: WARNING: Not setting tab completion: Profile file does not exist at 

    test: 'C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'.

    test: Chocolatey (choco.exe) is now ready.


Comments are closed.