---
title: PowerShell Menu for Azure CLI Bastion
description: A simple PowerShell menu for Azure Bastion Command Line Interface (CLI) RDP
---

[TEAM IM Insights ](https://www.teamim.com/insights)

# [PowerShell Menu for Azure CLI Bastion](https://www.teamim.com/insights/powershell-menu-for-azure-cli-bastion)

 Written by [Dwayne Parkinson](https://www.teamim.com/insights/author/dwayne-parkinson) | Apr 3, 2025 3:53:49 PM

If you're using Microsoft Azure, and the Bastion Platform As A Service (PaaS), there are a number of ways to connect to a server.  One of those ways is through the  
[Azure Network Bastion RDP ](https://learn.microsoft.com/en-us/cli/azure/network/bastion?view=azure-cli-latest#az-network-bastion-rdp)command.   A significant advantage of using this connection method is that it provides for easier copy and paste of files between your local machine and the server.

To use the command, the first thing you'll need to do is install the Azure Command Line Interface (CLI).  Those instructions can be found [here.](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli)  After installing Azure CLI you will need to set your subscription and then you can issue the ["az network bastion rdp"](https://learn.microsoft.com/en-us/cli/azure/network/bastion?view=azure-cli-latest#az-network-bastion-rdp) command in a terminal session on your PC to connect to your Azure servers.  

Here's where the PowerShell menu comes in.  Most folks will set up commands to connect to their servers and store them in a text file.  To connect they will open a terminal session and copy and paste the command.  Rather than do that, I decided to put the connection information into a PowerShell script so I can just run the script and select an option from a simple text menu.

You can create your own version of this menu by copying the code below and  saving it as a ps1 file (PowerShell script) on your computer.  By changing a few variables to include your Azure Tenant, Subscription and Server information you'll be up and running in no time. 

Everything you need to change is inside of [brackets] and details of how to make the changes are commented in the code below.  I've made some of the command parameters global and only the name and ID specific to the server, however any number of parameters can be moved into the PSCustomObject sections as needed.  

Hopefully this helps make life just a bit easier.  Enjoy.

`# ****** PREREQUISITES *******`

`# 1.  You must have Azure CLI set up on your computer.  https://learn.microsoft.com/en-us/cli/azure/install-azure-cli`

`# ****** USING THIS SCRIPT *******`

`# 1. Copy this file to your desktop and save with a .ps1 extension`

`# 2. Right click on the file and select "Run With PowerShell"`

`# 3. Select option "T" to authenticate and set your credentials for the Azure Tenant.  You may need to redo this periodically.`

`# 4. Select the option of the server you want to connect to.`

`# 5. When the Remote Desktop session starts, be sure to go to the Display tab and uncheck "Use All Monitors"`

`# ****** ADDING SERVERS *******`

`# If you need a server that isn't on the list you can add your own.  `

`# Each PSCustomObject block below represents a server you can connect to.`

`# To add a server to the list, copy an existing PSCustomObject block and change the Name and ID to the server you want.`

`#   NAME - Any name you want to give the server.  It is the name that will be displayed on the menu.  It does not need to match anything in Azure.`

`#   ID - This is the "ID" assigned by Azure.  To find this go to your Azure Compute Infrastructue. Select the server and click on the "JSON View" link in the right corner of the screen to retrieve the JSON.  The ID is the second item listed.`

`#        The ID will look something like this: /subscriptions/f2f34761-bda9-4292-868b-ax3304e62958/resourceGroups/rg-Servers-01/providers/Microsoft.Compute/virtualMachines/MYSERVER01`

`# ****** TROUBLESHOOTING ******`

`# If using Windows 11 there are restrictions on executing Power Shell scripts.  If the script isn't executing here are things to try.`

`#    1. Right click on the Windows Menu icon and select Terminal (Admin)`

`#    2. In the PowerShell terminal type: Get-ExecutionPolicy`

`#    3. If it's not RemoteSigned, then type: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine  `

`#    4. Try again`

`#    5. If it still doesn't work, in the PowerShell terminal, assuming this is on your desktop, type: unblock-File -Path "c:\users\[yourUser]\desktop\BastionRDPMenu.ps1" (or whatever you named it).`

`#    6. Try again`

`$global:Servers = @(`

`    [PSCustomObject]@{`

`        Name="[My Server Name 1]";`

`        ID="[subscription id 1]";`

`        },`

`    [PSCustomObject]@{`

`        Name="[My Server Name 2]";`

`        ID="[subscription id 2]";`

`        },`

`    [PSCustomObject]@{`

`        Name="[My Server Name 3]";`

`        ID="[subscription id 3]";`

`        },`

`    [PSCustomObject]@{`

`        Name="[My Server Name 4]";`

`        ID="[subscription id 4]";`

`        }`

`)`

`# ================= DO NOT CHANGE ANYTHING BELOW HERE WHEN ADDING A SERVER ========================`

`# ***** Global Azure settings *****`

`$global:SubInfo="[MyServerSubInfo]"`

`$global:ResourceGroup="[MyResourceGroup]"`

`$global:Tenant="[MyTenant]"`

`$global:Subscription="[MyAzureSubscription]"`

`                                                                                 `

`$global:DisplayWarning="`n`n*************************************** REMINDER ****************************************`n** On the RDP Client go to the Display tab and uncheck the option to use all monitors. **`n*****************************************************************************************`n`n"`

`$global:Connecting="`n Launching RDP Client and connecting to "`

`function Show-Menu {`

`    cls`

`    Write-Host "=========== RDP Access Menu ============"`

`    $count = @($global:Servers).Count`

`    for ($i = 0; $i -le $count-1; $i++) {`

`        $x=$i+1`

`        Write-Host ("$x. "+$global:Servers[$i].Name)`

`        }`

`    Write-Host "T: Set Azure Tenant Credentials"`

`    Write-Host "Q: Quit"`

`    Write-Host "========================================"`

`}`

`function Execute-Option {`

`    param (`

`        [string]$Option`

`    )`

`    cls`

`    if ($Option -eq 'T') {`

`        Write-Host "Choosing tenant"`

`        az login --tenant $global:Tenant`

`        Write-Host "Setting subscription"`

`        az account set --subscription $global:Subscription`

`        pause`

`        }`

`   `

`    elseif ($Option -eq 'Q') {`

`        return $FALSE`

`        }`

`    elseif ($Option -as [int]) {`

`        $i=[int]::Parse($Option)`

`        $count = @($global:Servers).Count`

`        if ($i -gt 0 -and $i -le $count+1) {`

`            $x=$i-1`

`            Write-Host ($global:Connecting + $global:Servers[$x].Name + $global:DisplayWarning)`

`            $azCommand="az network bastion rdp --name """+$global:SubInfo+""" --resource-group """+$global:ResourceGroup+""" --target-resource-id """+$global:Servers[$x].ID+"""  --configure"`

`            Invoke-Expression $azCommand`

`            }  `

`        else { # Numeric option out of range`

`            Write-Host ($Option + " <-- Invalid numeric selection, please try again.")`

`            pause`

`            }`

`        }  `

`    else { # Non-numeric option other than Q or T`

`        Write-Host ($Option + " <-- Invalid selection, please try again.")`

`        pause`

`      }`

`    return $TRUE`

`}`

`do {`

`    Show-Menu`

`    $selection = Read-Host "Please make a selection"`

`    $continue = Execute-Option -Option $selection`

`} while ($continue)`

 

[View full post](https://www.teamim.com/insights/powershell-menu-for-azure-cli-bastion)

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Dwayne Parkinson"
  },
  "dateModified" : "2025-04-03T15:53:49.399Z",
  "datePublished" : "2025-04-03T15:53:49Z",
  "headline" : "PowerShell Menu for Azure CLI Bastion",
  "image" : {
    "@type" : "ImageObject",
    "height" : 60,
    "url" : "/hs/hsstatic/content_shared_assets/static-1.4092/img/default-amp-logo.png",
    "width" : 60
  },
  "mainEntityOfPage" : "https://www.teamim.com/insights/powershell-menu-for-azure-cli-bastion",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "height" : 60,
      "url" : "/hs/hsstatic/content_shared_assets/static-1.4092/img/default-amp-logo.png",
      "width" : 60
    },
    "name" : "TEAM IM Insights"
  }
}
```