Occasionally you may need to run Powershell scripts for maintenance, troubleshooting/recovery or to find out a certain setting without the need to RDP into a VM or if you do not have access to the VM at the OS level, this can be achieved using the Run Command within the Azure portal under the VM plane.

The run command uses the VM agent to run the script inside the virtual machine.

To run a Powershell script complete the following steps,

Navigate to the VM in question, under Operations > Run command select RunPowerShellScript (Check to see if some of the sample scripts provided meet your needs and run those instead)

For this example, I will simply be checking the service status of windows updates, enabling the startup type to automatic and starting the service.

Firstly check the status by copy/paste the following command then select Run

Get-Service wuauserv | Select-Object -Property Name, StartType, Status 

 As you can see from the output the startup type is Disabled and the service is stopped

The following 3 lines of code, set the service to start automatically, start the service then checks the status

Set-Service -Name wuauserv -StartupType Automatic 
Start-Service -Name wuauserv
Get-Service wuauserv | Select-Object -Property Name, StartType, Status