{"id":179,"date":"2025-02-26T13:24:52","date_gmt":"2025-02-26T21:24:52","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/?p=179"},"modified":"2025-02-26T13:27:31","modified_gmt":"2025-02-26T21:27:31","slug":"using-powershell7-with-managed-runcommand","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/using-powershell7-with-managed-runcommand\/","title":{"rendered":"Using Powershell7 with Managed Runcommand"},"content":{"rendered":"<p>Today, all scripts run through Managed RunCommand will by default use Powershell 5. What if you have a script that requires Powershell7? This is supported via a new feature, but you will need to specify the different script shell.<\/p>\n<p>Here&#8217;s what you need to do.<\/p>\n<p><strong>Ensure your VM has Powershell7<\/strong><\/p>\n<p>Powershell7 is not installed by default. To ensure it&#8217;s available on your machine, you have the following options.<\/p>\n<ol>\n<li>Create a VM image with Powershell7 pre-installed and use that to create your VMs. For more information on creating images, see <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/virtual-machines\/image-builder-overview?tabs=azure-powershell\">here<\/a>. For information on sharing images, see <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/virtual-machines\/shared-image-galleries?tabs=vmsource%2Cazure-cli\">here<\/a>.<\/li>\n<li>Use RunCommand to install Powershell7 on your VM.<\/li>\n<\/ol>\n<p>Here&#8217;s an example on how to do this:<\/p>\n<p><code>az vm run-command create --name \"blah\" --vm-name $vmname--resource-group $rg --script \"Invoke-WebRequest -Uri https:\/\/github.com\/PowerShell\/PowerShell\/releases\/download\/v7.4.6\/PowerShell-7.4.6-win-x64.msi -OutFile PowerShell-7.msi; Start-Process msiexec.exe -ArgumentList '\/i PowerShell-7.msi \/quiet \/norestart' -Wait;Remove-Item -Path PowerShell-7.msi\" --location $location<\/code><\/p>\n<p>By the time you read this, you&#8217;ll likely need to update the download of Powershell7, which can be found <a href=\"https:\/\/learn.microsoft.com\/en-us\/powershell\/scripting\/install\/installing-powershell-on-windows?view=powershell-7.4\">here<\/a>.<\/p>\n<p>To verify that everything installed, check the instance view. Note that you may need to open an outbound networking rule for this to work.<\/p>\n<p><code>az vm run-command show --name \"blah\" --vm-name $vmname --resource-group $rg --expand instanceView<\/code><\/p>\n<p>3. Download the package yourself and create a VmApplication from it. More detail on VM Applications can be found <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/virtual-machines\/vm-applications?tabs=ubuntu\">here<\/a>.<\/p>\n<p>This option is more complicated, but is useful in situations where your VM is locked down and you don&#8217;t wish to open the outbound connection. Since VM Applications are downloaded via the host as a proxy, there&#8217;s no need to open any ports.<\/p>\n<p><strong>Run your script using Powershell7<\/strong><\/p>\n<p>As of this writing, this feature isn&#8217;t yet supported in the Portal (which only supports Action RunCommand and not Managed RunCommand), or via CLI and Powershell, where the necessary parameter isn&#8217;t supported. You&#8217;re therefore going to need to use an ARM template. Here is an example.<\/p>\n<p><code>{<\/code>\n<code>\u00a0 \u00a0\"$schema\": \"https:\/\/schema.management.azure.com\/schemas\/2019-04-01\/deploymentTemplate.json#\",<\/code>\n<code>\u00a0 \u00a0\"contentVersion\": \"1.0.0.0\",<\/code>\n<code>\u00a0 \u00a0\"resources\": [<\/code>\n<code>\u00a0 \u00a0{<\/code>\n<code>\u00a0 \u00a0 \u00a0 \"type\": \"Microsoft.Compute\/virtualMachines\/runCommands\",<\/code>\n<code>\u00a0 \u00a0 \u00a0 \"apiVersion\": \"<strong>2024-07-01<\/strong>\",<\/code>\n<code>\u00a0 \u00a0 \u00a0 \"name\": \"[concat(parameters('vmName'), '\/RunPowershellSeven')]\",<\/code>\n<code>\u00a0 \u00a0 \u00a0 \"location\": \"[parameters('region')]\",<\/code>\n<code>\u00a0 \u00a0 \u00a0 \"dependsOn\": [],<\/code>\n<code>\u00a0 \u00a0 \u00a0 \"properties\": {<\/code>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\"asyncExecution\": false,<\/code>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\"source\": {<\/code>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"script\": \"Write-Output $PSVersionTable\",<\/code>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<strong> \"scriptShell\": \"Powershell7\"<\/strong><\/code>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0},<\/code>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\"parameters\": []<\/code>\n<code>\u00a0 \u00a0 \u00a0 \u00a0}<\/code>\n<code>\u00a0 \u00a0}<\/code>\n<code>\u00a0 \u00a0],<\/code>\n<code>\u00a0 \u00a0\"parameters\": {<\/code>\n<code>\u00a0 \u00a0 \u00a0 \"vmName\": {<\/code>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\"type\": \"string\",<\/code>\n<code>\u00a0 \u00a0 \u00a0 },<\/code>\n<code>\u00a0 \u00a0 \u00a0 \"region\": {<\/code>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\"type\": \"string\"<\/code>\n<code>\u00a0 \u00a0 \u00a0 }<\/code>\n<code>\u00a0 \u00a0}<\/code>\n<code>}<\/code><\/p>\n<p>Note the ScriptShell property that indicates the command should be run on Powershell7. Also note that the apiVersion must be at least 2024-07-01 for this to work.<\/p>\n<p>To run this command on a VM, you then may run:<\/p>\n<p><code>New-AzResourceGroupDeployment -ResourceGroupName $rg -TemplateFile $templateFilePath -vmName $vmname -region $region<\/code><\/p>\n<p>And again to validate that the command ran:<\/p>\n<p><code>az vm run-command show --name \"RunPowershellSeven\" --vm-name $vmname --resource-group $rg --expand instanceView<\/code><\/p>\n<p>Of course your script doesn&#8217;t (and shouldn&#8217;t typically be) in line. All of the standard Managed RunCommand properties that download a script are valid.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, all scripts run through Managed RunCommand will by default use Powershell 5. What if you have a script that requires Powershell7? This is supported via a new feature, but you will need to specify the different script shell. Here&#8217;s what you need to do. Ensure your VM has Powershell7 Powershell7 is not installed by [&hellip;]<\/p>\n","protected":false},"author":103696,"featured_media":184,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,6],"tags":[17,9],"class_list":["post-179","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-vm-runtime","category-runcommand","tag-powershell7","tag-runcommand"],"acf":[],"blog_post_summary":"<p>Today, all scripts run through Managed RunCommand will by default use Powershell 5. What if you have a script that requires Powershell7? This is supported via a new feature, but you will need to specify the different script shell. Here&#8217;s what you need to do. Ensure your VM has Powershell7 Powershell7 is not installed by [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/posts\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/users\/103696"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/comments?post=179"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/posts\/179\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/media\/184"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/media?parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/categories?post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/tags?post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}