
Azure PowerShell is a command line interface(CLI) tool is used to manage and automate Azure resources. It can also be used to interact with Azure services and resources, including virtual machines, storage accounts, and databases. It offers a set of cmdlets. This article will help you learn the various ways of running PowerShell remotely on Azure Virtual machines. With a special focus on the Run Command and Custom Script Extension for VM. Other useful articles can be found here in the following guides. How to Enable Two-Factor Authentication for SSH in Linux, how to use Azure Compute Gallery, Azure Monitor, how to Add a User to the Sudoers List in Linux and how to install and Uninstall Discord in a Linux System.
Requirements to run PowerShell remotely on Azure VMs
To be able to follow this guide, there are certain things that are needed and some are listed below. He is another interesting guide on how to set up a third-party DNS server on a Linux Server.
- A tenant and subscription to Microsoft Azure with the necessary rights, such as Owner role or Contributor role
- CLI for PowerShell & Azure and Powershell modules
- Azure Virtual Machine
Executing Run Command in Azure Portal
On an Azure Windows VM, execute the following PowerShell script using Run Command:
- In the Azure portal, deploy your windows virtual machine
- Navigate to the virtual machine resource that you deployed in step 1
- From the list of menu items on the left side of the portal, Select Operations > Run Command from the menu. From the list of commands, pick RunPowerShellScript.

Next, in the Run Command Script pane, we typed the PowerShell script text that we want to execute on the server. The example that follows installs IIS, When finished, click the run button as shown below

While executing, it shows there is a message at the top of the run screen to show that “Script execution in progress…” However, The output of the script we ran is as shown below:

Executing Run Command in Azure Powershell
Run Command can also be accessed through PowerShell by using the invoke-AzVMrunCommand
function. This command is a component of the collection of Azure PowerShell modules called Az.Compute
Run a PowerShell application using this command:
- click on cloud shell and open the PowerShell window
- if you are not using the cloud shell make sure you log in to your Azure tenant Log into your Azure using the
Connect-AzAccount
command. - Specify the –
ResourceGroupName
and –VMName
of the virtual machine when using theInvoke-AZVmRunCommand
and Scriptpath and finally, the command-Id
Sample code
Invoke-AzVmRunCommand `
-ResourceGroupName "myCoolrg" `
-VMName "techdirectvm" `
-CommandId "RunPowerShellScript" `
-ScriptPath "C:\ps\Install-IIS.ps1"

Executing the run command with Azure CLI
When running PowerShell remotely on Azure VMs, PowerShell programs and scripts can be executed using the Azure CLI’s az vm run-command invoke command. To execute a PowerShell script, use the following command:
- Use Az login if you are accessing from outside the cloud shell on the terminal and make sure you have a valid subscription and are in an Azure tenant
- Use the —resource-group and —name parameters of the az vm run-command invoke to define the virtual machine’s name and resource group. Use the At (@) symbol to indicate the script file name when using RunPowerShellScript’s —command-id parameter.
Notice how the path name contains two backslashes (). The Install-IIS.ps1 script used in this example has the same code as the ones used in the earlier instances.
Sample code
az vm run-command invoke \ --resource-group 'myCoolrg' \ --name 'techdirectvm' \ --command-id 'RunPowerShellScript' \ --scripts @C:\\ps\\Install-IIS.ps1

Azure CLI has some advantages over PowerShell, including the capacity to execute single instructions rather than scripts. You can send specific PowerShell commands in place of a script in the —scripts argument.
Running PowerShell Remotely on Azure VM Using Custom Script Extension
Utilizing a virtual machine with the Custom Script Extension is an additional choice. For post-deployment configurations, the Custom Script Extension gets and runs scripts on Azure virtual machines. The PowerShell scripts are put in a spot where the Azure virtual machine can route to them, like GitHub, Azure Blog storage, or a local file server.
For the VM to be able to view the file, you might need to add firewall rules or Network Security Group exceptions. So, let’s assume that we have a project that we are working on. We have decided to create a storage account and upload the script that we intend to use for our virtual machine to the storage account, here is the code we used for this:
# Define the storage account and container parameters
$storageAccountName = "techdirectscripts"
$storageAccountKey = "QvNwlr05BCGFk8cDhSEbP38pSqfbTQY2wjmwDCcYIdgedfID9/VudqSHcIaFOXmMZhIbHKUlHQnY+AStyoS+5w=="
$containerName = "file1"
$blobName = "install-webserver.ps1"
$localFilePath = "C:\Users\Raph\storage\install-webserver.ps1"
# Connect to the storage account
$ctx = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
# Create the container
New-AzStorageContainer -Name $containerName -Context $ctx
# Upload the script file to the container
Set-AzStorageBlobContent -Container $containerName -Blob $blobName -File $localFilePath -Context $ctx
Output


Here is the content of the script uploaded to Azure storage & what we need to run
# Define the virtual machine and script parameters
$vmName = "techdirectvm"
$resourceGroup = "myCoolrg"
$scriptName = "install-webserver.ps1"
$scriptURI = "https://mystorageaccount.blob.core.windows.net/scripts/$file1"
# Add the Custom Script Extension to the virtual machine
Set-AzVMCustomScriptExtension -ResourceGroupName myCoolrg `
-VMName techdirectvm `
-Location "East US" `
-FileUri https://techdirectscripts.blob.core.windows.net/boomscripts/file1 `
-Run "install-webserverscript" `
-Argument "-webServerName techd2" `
-Name "CustomScript"
# Start the virtual machine
Start-AzVM -ResourceGroupName myCoolrg -Name techdirectvm
What is left is just to run the “Add the Custom Script Extension to the virtual machine” code above and start the virtual machine.
Executing from Storage Account Blob
Similar to the example above, we have to connect to the storage account created and had access to the content of the script through the storage URL. In Summary, to execute from a storage account blob, the PowerShell script’s blob URI should be placed in the same $fileUris array variable.
Next, Add storageAccountName and storageAccountKey data and others. Lastly, run Set-AzVMExtension, and update the name of the server, resource group, and deployment-specific address as necessary.
Running PowerShell remotely on Azure VMS: Summary
In this article, we analyzed the various there are ways to run a PowerShell script with regards to a virtual machine and some of them are by running directly from the virtual machine using the run command.
By using Azure PowerShell in or outside the portal by running the Invoke-AzVmRunCommand, by using Azure CLI and running the az vm run-command
invoke and finally, we talked about uploading the script to Azure cloud storage and running the Set-AzVMCustomScriptExtension.
I hope you found this blog post helpful. If you have any questions, please let me know in the comment session.