Terraform using user_data to run post setup command on a Windows machine
16:39 26 Nov 2025

New to Terraform. I am just learning via implementing a process our company does manually which is a simple image build and deploy eventually to be used as a module.

I have created the image via learning Packer, all the company security software is on that gallery image which I am deploying via TF. Im have been trying for two days to get the powershell script to run after the TF deployment using user_data. At the moment I am just trying to get the below code to work via that method, once that executes correctly I can amend the PS script to do what I need.

New-Item -Path "C:/temp/" -Name "Logfiles01" -ItemType "Directory"

I have been able to achieve the above with azurerm_virtual_machine _extensions but from what I am reading I can only have 1 in my .tf and thats already occupied by a DomainJoin extension and dont want to move that DomainJoin over to the powershell method.

Ive tried following these among others with no success and amending for Windows where neccesary. :-

https://www.solutioning.eu/2023/03/06/azure-terraform-error-expected-user_data-to-be-a-base64-string-got-usr-bin-bash/
https://realworldit.net/archives/923#:~:text=The%20user_data%20field%20contains%20a,during%20the%20instance%20bootstrap%20process.
https://groups.google.com/g/terraform-tool/c/lHG1teqPneY/m/7xJXcrMDAwAJ

In Terraform, how to pass variables to powershell template file?

https://learning-ocean.com/tutorials/terraform/terraform-user-data/

Also I have tried these formats but can not get it to work.

user_data = "${base64encode(file("${path.module}/templateFile.ps1"))}"
user_data = "${base64encode(file("${path.module}/templateFile.ps1"))}"


user_data = "${base64encode(file("C:\\temp\\postDeployConf.ps1"))}"



user_data    = base64encode(data.template_file.ps-scripts.rendered)# 

data "template_file" "ps-scripts" {
   template = file("./templateFile.ps1")
}
user_data = <<-EOF 
 
New-Item -Path "C:/temp/" -Name "Logfiles01" -ItemType "Directory"
 
EOF 

Anyone have any insight to where I may be going wrong. I tried setting up logging via a TF_LOG environmental variable but its not helping at all.

Thanks

terraform terraform-provider-azure user-data