---
title: "Run Scripts on Azure VMs with Custom Script Extension"
description: "Remotely run PowerShell scripts on your Azure VMs using the Azure custom script extension for Windows."
canonical: "https://adamtheautomator.com/azure-run-script-on-vm/"
---

# Run Scripts on Azure VMs with Custom Script Extension

> Remotely run PowerShell scripts on your Azure VMs using the Azure custom script extension for Windows.

Source: https://adamtheautomator.com/azure-run-script-on-vm/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Run Scripts on Azure VMs with Custom Script Extension](https://adamtheautomator.com/wp-content/uploads/2019/07/iot-3337536_1280.png)

# Run Scripts on Azure VMs with Custom Script Extension

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)21 July 20193 min. read

Categories: [Cloud](/category/cloud/)

Tags:[Azure Virtual Machines](/tag/azure-virtual-machines/)[Microsoft Azure](/tag/microsoft-azure/)[PowerShell](/tag/powershell/)

Table of Contents

*   [Using Azure to Run a Script on a VM](#building-an-azure-custom-script-extension-for-windows)
*   [Uploading a PowerShell Script to Azure](#uploading-a-powershell-script-to-azure)
*   [Running the Custom Script Extension](#running-the-custom-script-extension)
*   [Summary](#summary)

As your team begins to provision and configure more Windows [Azure](https://adamtheautomator.com/tag/microsoft-azure/) virtual machines (VMs), at some point, you’ll get tired of reinventing the wheel. You need automation! In this article, come with me as I walk you through step-by-step to begin using [Azure custom script extensions](https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows) to use Azure to run scripts on your VMs.

If your team is in a Windows environment, one of the best tools to automate server configuration is PowerShell. By using the Azure PowerShell module, your organization can leverage the power of PowerShell not only to automate on-prem tasks but also to run commands on Azure VMs in bulk rather than one at a time.

One feature Microsoft has provided us is called the Azure custom script extension. The custom script extension is an Azure virtual machine extension that the VM agent runs to execute arbitrary PowerShell code against your VMs by using the Azure API rather than consoling into the VM or using [PowerShell remoting](https://adamtheautomator.com/psremoting/ "PowerShell remoting").

Running commands this way provides several benefits. Running commands using the Azure custom extension in Windows:

*   provides increased security by not needing to open network ports for PowerShell remoting
*   allows for easy executing PowerShell code at VM startup
*   automatically transferring resources from Azure storage to your VM as part of the provisioning process
*   an easy way of running PowerShell scripts stored in various Azure storage accounts

Enabling an Azure custom script extension for Windows can be done in a few ways. In this article, we’ll be focusing on enabling the custom script extension via PowerShell, but you may also enable the extension via an Azure Resource Manager(ARM) template.

As a simple example, let’s say you’d like to ensure PowerShell remoting is enabled on an Azure VM in your subscription. To do this, you’d need to run the following command locally on each VM:

```powershell
Enable-PSRemoting -Force
```

Let’s build a custom script extension to do this.

## Using Azure to Run a Script on a VM

First, create a PowerShell script called _Enable-PSRemoting.ps1_ on your local computer with the command above inside. This script needs to run on an Azure VM. To do this, we’ll build another small PowerShell script called _New-CustomScriptExtension.ps1_ to get it uploaded into Azure and a custom script extension created to execute it. Before we get too far, you’ll need a couple items:

*   The Azure resource group and storage account name that will store the script
*   The Azure storage container name
*   The VM name
*   The Azure resource group name that the VM is in

This script can be broken down into two sections; getting the small PowerShell script uploaded to Azure and creating the custom script extension. Everything else is required to glue these two processes together.

## Uploading a PowerShell Script to Azure

First, we’ll upload the _Enable-PSRemoting.ps1_ script to the Azure storage account (`$StorageAccountName`) inside of the container (`$ContainerName`) in the resource group (`$ResourceGroupName`).

```powershell
$saParams = @{
    'ResourceGroupName' = $ResourceGroupName
    'Name' = $StorageAccountName
}
$storageContainer = Get-AzureRmStorageAccount @saParams |Get-AzureStorageContainer -Container $ContainerName
$bcParams = @{
    'File' = 'C:\Enable-PSRemoting.ps1'
    'BlobType' = 'Block'
    'Blob' = ' Enable-PSRemoting.ps1'
}
$storageContainer | Set-AzureStorageBlobContent @bcParams
```

## Running the Custom Script Extension

When you run _New-CustomScriptExtension.ps1_, this script will upload the _Enable-PSRemoting.ps1_ script to the Azure storage account specified.

Now that the script is stored in Azure, you can execute it via the custom script extension for the VM:

*   Name: `$VMName`
*   Resource Group: `$rgName`
*   Storage Account: `$saName`
*   Storage Container: `$scName`

Open up a text editor for _New-CustomScriptExtension.ps1_ and paste in the below example. When you run this, it will execute the Azure custom script extension for Windows which will execute the _Enable-PSRemoting.ps1_ PowerShell script you uploaded earlier.

```powershell
# Get the VM we need to configure
$vm = Get-AzureRmVM -ResourceGroupName $rgName -Name $VMName
# Get storage account key
$key = (Get-AzureRmStorageAccountKey -Name $saName -ResourceGroupName$rgname).Key1
## Enable the custom script extension and run the script
$scriptParams = @{
    'ResourceGroupName' = $rgName
    'VMName' = $VMName
    'Name' = 'Enable-PSRemoting.ps1'
    'Location' = $vm.Location
    'StorageAccountName' = $saName
    'StorageAccountKey' = $key
    'FileName' = 'Enable-PSRemoting.ps1'
    'ContainerName' = $scName
    'Run' = 'Enable-PSRemoting.ps1'
}
Set-AzureRmVMCustomScriptExtension @scriptParams
```

## Summary

Once this script completes, you can verify that _Enable-PSRemoting.ps1_ has executed on the VM and PowerShell remoting has been successfully enabled. You should now be able to use [`Invoke-Command`](https://adamtheautomator.com/invoke-command/ "Invoke-Command") against your Azure VM.

By leveraging the Azure custom script extension in Windows, you now have the ability to remotely run any kind of PowerShell scripts on your Azure VMs.

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fazure-run-script-on-vm%2F&text=Run%20Scripts%20on%20Azure%20VMs%20with%20Custom%20Script%20Extension)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fazure-run-script-on-vm%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fazure-run-script-on-vm%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2019/12/network-2402637_1280.jpg)

### [Optimize Azure Scale Sets Management with PowerShell](/azure-powershell-scale-set/)

Master Azure Scale Sets management using PowerShell. Learn to create, configure, and automate VM scale sets for improved cloud infrastructure.

![](https://adamtheautomator.com/wp-content/uploads/2019/07/5d238ae5c824b514689ea66d.jpg)

### [Delete Azure VMs & Clean Up with PowerShell](/azure-vm-delete/)

Learn how to delete an Azure VM and clean up related resources using the Remove-AzVM PowerShell command.

![](https://adamtheautomator.com/wp-content/uploads/2019/07/5d238ae5c824b514689ea64e.jpg)

### [Effortless Azure VM Snapshots with PowerShell](/azure-vm-snapshot/)

Create, manage, restore, and remove Azure VM snapshots using a community PowerShell module in just a few keystrokes!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
