DEV Community

Cover image for Using PowerShell with Azure Cloud Shell
Michael Bender for Microsoft Azure

Posted on

Using PowerShell with Azure Cloud Shell

We've selected our favorite tips and tricks created by Michael Crump as well as planned fresh technical content on Azure all April! Miss a day (or more)? Catch up with the series.

Don't have Azure? Grab a free subscription.

Azure Cloud Shell includes two command line languages for you to use: PowerShell and Bash. Traditionally, Bash has been the preferred method within Cloud Shell due to the rich set of tools in Azure CLI. With the update of the Azure PowerShell tools, there is feature parity between the two options so let's take a look at PowerShell in Azure Cloud Shell.

Accessing PowerShell in Azure Cloud Shell

To access PowerShell in Azure Cloud Shell, you have a few options.

  1. You can type pwsh in the shell, and you'll be authenticated into an instance of Cloud Shell with PowerShell without leaving the current Cloud Shell session.
    Switching from Bash to PowerShell in Cloud Shell

  2. Another option is to re-launch the Cloud Shell window by choosing PowerShell from the dropdown menu
    Choosing PowerShell from Dropdown

and choosing 'Confirm' to restart. This will reconnect your session to the current container using PowerShell.
Restart Session with PowerShell

Once you are authenticated, you'll be able to explore the resources available in Azure using PowerShell.

To verify the version of PowerShell you are running, type $PSVersionTable.



PS Azure:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.2.0
PSEdition                      Core
GitCommitId                    6.2.0
OS                             Linux 4.15.0-1041-azure #45-Ubuntu SMP Fri Mar 15 14:41:00 UTC 2019
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0



Enter fullscreen mode Exit fullscreen mode

Notice that the OS for the Cloud Shell instance is Linux. That is because cloud shell uses PowerShell Core inside of a Linux container. Many of the same tools you would use in PowerShell Core on your machine are available as you can see with all of the installed modules.




PS Azure:\> Get-Module
ModuleType Version    Name                                ExportedCommands---------- -------    ----                                ----------------Script     1.5.1      Az.Accounts                         {Add-AzEnvironment, Clear-AzContext, Clear-AzDefault, Connect-AzAccount}Script     1.8.0      Az.Compute                          {Add-AzContainerServiceAgentPoolProfile, Add-AzImageDataDisk, Add-AzVhd, Add-AzVMAdditi
Script     1.7.0      Az.Network                          {Add-AzApplicationGatewayAuthenticationCertificate, Add-AzApplicationGatewayBackendAddr
Script     1.3.1      Az.Resources                        {Add-AzADGroupMember, Export-AzResourceGroup, Get-AzADAppCredential, Get-AzADApplicatio
Script     1.2.0      Az.Storage                          {Add-AzRmStorageContainerLegalHold, Add-AzStorageAccountManagementPolicyAction, Add-AzS
Script     0.0.0.9    AzureAD.Standard.Preview            {Add-AzureADAdministrativeUnitMember, Add-AzureADApplicationOwner, Add-AzureADApplicati
Script     0.9.3      AzurePSDrive
Manifest   6.1.0.0    Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-ItemProperty}
Manifest   6.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object}
Script     0.9.3      PSCloudShellUtility                 {Disable-AzVMPSRemoting, Dismount-CloudDrive, Enable-AzVMPSRemoting, Enter-AzVM}
Script     2.0.0      PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PSReadLineKeyHandler, Set-PSRea
Binary     0.8.1      SHiPS



Enter fullscreen mode Exit fullscreen mode

Also, you have built-in tools like the Azure PowerShell module for managing your resources in Azure.

To view all the Azure PowerShell commands, type Get-Command -Module AZ* | more



PS Azure:\> Get-Command -Module AZ* | more

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           Add-AdlAnalyticsDataSource                         1.0.0      Az.DataLakeAnalytics
Alias           Add-AdlAnalyticsFirewallRule                       1.0.0      Az.DataLakeAnalytics
Alias           Add-AdlStoreFirewallRule                           1.2.0      Az.DataLakeStore



Enter fullscreen mode Exit fullscreen mode

What's great about this is that you don't have to worry about updating these tools; It's all done for you.

Exploring Azure Drive

When you log into Cloud Shell, you'll have access to Azure Drive. Azure Drive gives you file system-like access to all your tenant resources. It does this by building a PowerShell Drive, based on SHiPS (Simple Hierarchy in PowerShell). So you can do a directory listing and see all of your subscriptions like this:



Azure:/
PS Azure:\> dir

    Directory: Azure:

Mode SubscriptionName                     SubscriptionId                       TenantId                             State
---- ----------------                     --------------                       --------                             -----
+    ca-mibender-demo-test                xxxx-xxxx-xxxx-xxxx-xxxx xxxx-xxxx-xxxx-xxxx-xxxx  Enabled



Enter fullscreen mode Exit fullscreen mode

Then drill down into the directories to view resources deployed in Azure like Virtual Machines by using cd




Azure:/
PS Azure:\> cd ./ca-mibender-demo-test/VirtualMachines/
Azure:/ca-mibender-demo-test/VirtualMachines
PS Azure:\> dir
    Directory: Azure:/ca-mibender-demo-test/VirtualMachinesName              ResourceGroupName   Location          VmSize  OsType            NIC ProvisioningState  PowerState
----              -----------------   --------          ------  ------            --- -----------------  ----------
myVM        CLOUDSHELL-DEMO-MICHAEL westeurope     Standard_A1   Linux       myVM-nic         Succeeded     running
vm-linux-01   AZURE-CLOUDSHELL-DEMO    westus2 Standard_DS1_v2   Linux vm-linux-01336         Succeeded deallocated
vm-linux-02   AZURE-CLOUDSHELL-DEMO    westus2 Standard_DS1_v2   Linux vm-linux-02181         Succeeded     running
vm-win-01     AZURE-CLOUDSHELL-DEMO    westus2 Standard_DS1_v2 Windows   vm-win-01543         Succeeded     running
vm-win-02     AZURE-CLOUDSHELL-DEMO    westus2 Standard_DS1_v2 Windows   vm-win-02953         Succeeded     running



Enter fullscreen mode Exit fullscreen mode

Now if you want to see the properties of a VM, let's say vm-linux-01, you can use the Get-AzVm command for that



PS Azure:\> get-AzVm -Name vm-linux-01 

ResourceGroupName            Name Location          VmSize OsType            NIC ProvisioningState Zone
-----------------            ---- --------          ------ ------            --- ----------------- ----
AZURE-CLOUDSHELL-DEMO vm-linux-01  westus2 Standard_DS1_v2  Linux vm-linux-01336         Succeeded



Enter fullscreen mode Exit fullscreen mode

Accessing Azure Cloud Drive

Another feature of Cloud Shell is CloudDrive. CloudDrive is a 5GB Azure File Share that provides persistent storage to your Cloud Shell instance. This allows you to store scripts, install tooling, and other file-management tasks, and have all of this available every time you access Cloud Shell.

To get to your Cloud Drive, type in cd $Home to access the home drive where Cloud Drive resides. Then type cd ./CloudDrive to enter your persistent CloudDrive storage.



PS Azure:\> cd $home
PS /home/michael> dir

    Directory: /home/michael

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d----l           4/25/19  1:24 PM                clouddrive

PS /home/michael> cd ./clouddrive/
PS /home/michael/clouddrive> dir

    Directory: /home/michael/clouddrive

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----           4/15/19  2:13 PM                DemoDirectory
d-----           4/11/19  9:50 PM                scripts

PS /home/michael/clouddrive>


Enter fullscreen mode Exit fullscreen mode

And if you are still not sold on using PowerShell, you can still run your Bash, and Azure CLI commands in the PowerShell console. It supports both!




PS Azure:\> az group create --name myResourceGroup --location eastus
{
  "id": "/subscriptions/XXXX-XXXX-XXXX-XXXX/resourceGroups/myResourceGroup",
  "location": "eastus",
  "managedBy": null,
  "name": "myResourceGroup",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": null
}



Enter fullscreen mode Exit fullscreen mode

That will get you started using PowerShell in Azure Cloud Shell!

Want more PowerShell in Azure Cloud Shell? Check out our quickstarts and tutorials!


We'll be posting articles every day in April, so stay tuned or jump ahead and check out more tips and tricks now.

Top comments (1)

Collapse
 
omiossec profile image
Olivier Miossec

Great introduction, thank