Fixed issues with powershell script execution by ensuring environment variables are correctly set, appending newline characters to prevent formatting-related errors, and updating acceptance tests to use regex matching for more reliable assertions.
Starting with Packer v1.13.1, the way PowerShell commands are executed has changed. Packer now uses the -File operator to run PowerShell scripts by default. As part of this update, Packer prepends certain preamble logic to the script before adding your script content.
The default command now looks like:
powershell -executionpolicy <execution_policy> -file {{.Path}}
Here, {{.Path}} refers to the path of the temporary script file generated by Packer. When using the script or scripts block, Packer injects the preamble logic at the top of this script file.
If your script relies on specific PowerShell constructs (such as module imports, parameter declarations, or function definitions) that must appear at the top of the script, we recommend wrapping your script content inside a function and invoking it. This helps avoid any conflicts or ordering issues introduced by the preamble logic.
Fetched April 8, 2026