Editing PowerShell scripts in Azure Cloud Shell

Executing PowerShell scripts in Azure Cloud Shell is easy and fast, but how do you edit them before running? I will show you three ways to do that in this blog post.

What is Azure Cloud Shell?

“Azure Cloud Shell is an interactive, authenticated, browser-accessible terminal for managing Azure resources. It provides the flexibility of choosing the shell experience that best suits the way you work, either Bash or PowerShell.”

Source: https://learn.microsoft.com/en-us/azure/cloud-shell/overview

How do I get my scripts there?

You can store your scripts in your Home location, which is stored in a 5Gb File Share. This File Share is held in a Storage Account, which will be created the first time you start Azure Cloud Shell. (You have to specify an Azure Subscription for this) When this is all in place, you can upload your scripts from Azure Cloud Shell using the Upload/Download button. (You can start Azure Cloud Shell from the button bar in the top right of your Azure Portal (The PowerShell icon))

How do I edit the scripts?

Using the Built-In Editor

An Editor button (The one with the {} as the icon) will open the Editor, select the file you want to edit, and it should be displayed in the pane on the right. Alternatively, you can also start it by running the following:

code .

or by running the following to open the file directly:

code .\test.ps1

All three methods (Button bar, running code with or without file name) will result in this Editor screen:

In the top right of the Editor, you will see three dots. Clicking on it will give these options:

CTRL+Q will close the Editor, CTRL+P will open the Open File dialog, and F1 will show you the Command Palette as shown below: (Scroll down in the list for more, it’s a long list!)

Always nice to have keyboard shortcuts 🙂

If the Editor is idle for 20 minutes, it will exit the session, and you can reconnect it by selecting Reconnect:

And it will return to your session:

Using PSEdit

In an earlier blog post, I wrote about using PSEdit as an editor. This also applies to Azure Cloud Shell. Install it like this:

PS /home/admin> install-module PSEdit

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y

After installation, run PSEdit (or PSEdit ./test.ps1) to open PSEdit and edit scripts from there. It has IntelliSense, formatting, and debugging options and is more like VSCode.

Connecting Azure Cloud Shell from VSCode

Alternatively, Visual Studio Code can connect to Azure Cloud Shell. To do this, follow these steps:

  • Start Visual Studio Code
  • Press CTRL-Shift+X (Or click on Extensions on the left panel)
  • Search for “Azure Account” and install the Extension.
  • Search for “Azure Storage” and install that Extension too.
  • Press CTRL+Shift+P to open the list of commands.
  • Enter “Azure Sign In” and select it. A browser login should appear.
  • Login and “You are signed in now and can close this page.” should appear, and you can close the browser tab.
  • Press CTRL+Shift+P again to open the list of commands.
  • Search for “Azure Storage: Open in Explorer
  • Select the storage account for Azure Cloud Shell (In my case, it was cloud-shell-storage-westeurope), then select File Shares and your account from the pull-down menu. Select “Open in new Window” to show the files in a new tab.

You can add, delete, and modify files in Visual Studio Code from within this Window. To see or run the files from Azure Cloud Shell, browse to ./clouddrive/.cloudconsole. (The test2.ps1 was the file I edited from Visual Studio Code and was saved from there)

PS /home/admin> cd ./clouddrive/ 
PS /home/admin/clouddrive> cd ./.cloudconsole/
PS /home/admin/clouddrive/.cloudconsole> ls -ltra
total 5242880
drwxrwxrwx 2 admin admin          0 Apr 30 17:14 ..
drwxrwxrwx 2 admin admin          0 Apr 30 17:14 .
-rwxrwxrwx 1 admin admin          0 May 15 14:22 test2.ps1
-rwxrwxrwx 1 admin admin 5368709120 May 15 14:35 acc_admin.img

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.