Author : HASSAN MD TAREQ | Updated : 2021/12/01

PowerShell

“PowerShell” vs “Windows PowerShell”

Check PowerShell is intalled or not

Install Latest Version of PowerShell (not “Windows PowerShell”):

#
# Install latest version of PowerShell
#
# In the installation wizard, select "Do not close ..." -> you need to restart your PC
# 
Invoke-Expression "& { $(Invoke-Restmethod https://aka.ms/install-powershell.ps1) } -UseMSI"

# Short version of above command
# iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI"

Latest release: https://github.com/PowerShell/powershell/releases

Restart PC and check PowerShell is installed correctly

#
# Check current version
#
$PSVersionTable.PSVersion

PowerShellGet Module

#
# Check PowerShellGet module is installed or not
#
Get-Module -Name PowerShellGet -Listavailable

#
# If PowerShellGet module is not installed, then install PowerShellGet module
#
# Install/Update PowerShellGet module
# Check latest version of PowerShellGet: https://www.powershellgallery.com/packages/PowerShellGet
#
# If installation fails, try using follwoing flags (https://github.com/PowerShell/PowerShellGetv2/issues/599)
# -AllowClobber -SkipPublisherCheck
#
# Install-Module -Name PowerShellGet -RequiredVersion 2.2.5 -Force
#
Install-Module PowerShellGet -Repository PSGallery -Force

PowerShell in VS Code

PowerShell in Visual Studio

PowerShell Core (PowerShell 7) in Visual Studio Terminal

PowerShell Tools for Visual Studio (Paid tool)

Install PowerShell Tools for Visual Studio

Menu > View => PowerShell => PowerShell Interactive Window

PowerShell Interactive Window

PowerShell Azure Module

Azure Az module in PowerShell

#
# Check Az module is installed or not
# If not installed, nothing will be retured after executing the command
#
Get-InstalledModule -Name Az # Get-Module -Name Az -Listavailable


#
# Install Az module
#
# Install-Module -Name Az -Force -AllowClobber
#
# Default repository is PSGallery
# You can specify repository explicitly: -Repository PSGallery
#
Install-Module -Name Az -Force -AllowClobber -Scope AllUsers

#
# Check all versions of Az module that are installed in your PC
#
Get-InstalledModule -Name Az # Get-InstalledModule -Name Az -AllVersions -OutVariable AzVersions
Get-InstalledModule -Name "Az.*"

#
# Now uninstall AzureRM
#
Uninstall-AzureRm

Azure Az module in “Windows PowerShell”

#
# PowerShell script execution policy must be set to remote signed or less restrictive
# Get-ExecutionPolicy -List can be used to determine the current execution policy
#
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

#
# Using the Install-Module cmdlet is the preferred installation method for the Az PowerShell module
# Install the Az module for the current user only is the recommended installation scope
#
#Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force -AllowClobber
#
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force

#
# Now uninstall AzureRM
#
Uninstall-AzureRm

Using Latest PowerShell in PowerShell ISE

#
# Code snippet taken from: https://github.com/adamdriscoll/Presentations/blob/master/YouTube/Using%20PowerShell%207%20in%20the%20ISE/ps7.ps1
#

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Clear()
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Switch to PowerShell 7", { 
        function New-OutOfProcRunspace {
            param($ProcessId)

            $ci = New-Object -TypeName System.Management.Automation.Runspaces.NamedPipeConnectionInfo -ArgumentList @($ProcessId)
            $tt = [System.Management.Automation.Runspaces.TypeTable]::LoadDefaultTypeFiles()

            $Runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($ci, $Host, $tt)

            $Runspace.Open()
            $Runspace
        }

        $PowerShell = Start-Process PWSH -ArgumentList @("-NoExit") -PassThru -WindowStyle Hidden
        $Runspace = New-OutOfProcRunspace -ProcessId $PowerShell.Id
        $Host.PushRunspace($Runspace)
}, "ALT+F5") | Out-Null

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Switch to Windows PowerShell", { 
    $Host.PopRunspace()

    $Child = Get-CimInstance -ClassName win32_process | where {$_.ParentProcessId -eq $Pid}
    $Child | ForEach-Object { Stop-Process -Id $_.ProcessId }

}, "ALT+F6") | Out-Null

Azure CLI

PowerShell command to Install Azure CLI

Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .\AzureCLI.msi

Upgrade Azure CLI

az upgrade

Check version

az --version

Azure CloudShell

Accessing Cloudshell

Initial Setup

How to Use Manifest File in Command (i.e. to deploy K8s workload)

Re-mount fileshare

Connecting and running cloudsehll in VS Code

Running Azure CloudShell Locally

PowerShell launcher script

C:\az-cloudshell\cloudshell-launcer.ps1

$startShellType = "/usr/bin/pwsh" # PowerShell, for bash -> "/bin/bash"
$cloudshellImageName = "mcr.microsoft.com/azure-cloudshell"

$sourceScriptFolder = "`"C:\az-cloudshell\scripts`""  # the folder where your local scrips are located, double quote is required ('`' to escape double quote)
$targetScriptFolder = "/usr/cloudshell"       # mounted folder in container 

$container = $(docker ps -q --filter ancestor=$cloudshellImageName)
$containerIsRunning = ($container -ne $null)

if ($containerIsRunning) { 

    Write-Host "Container is running. Connecting to container..."

    docker exec -it $container $startShellType

} else { 

    Write-Host "Container is not running. Updating container image..."

    docker pull "$($cloudshellImageName):latest"

    Write-Host "Mapping '$($sourceScriptFolder)' to '$($targetScriptFolder)'. Connecting to cloudshell (interactive)... `r`n`r`n"
    
    $volumeMount = "$($sourceScriptFolder):$($targetScriptFolder)"

    docker run -it -v $volumeMount mcr.microsoft.com/azure-cloudshell $startShellType
}

Create shortcut with following

"C:\Program Files\PowerShell\7\pwsh.exe" -ExecutionPolicy Bypass -File "C:\az-cloudshell\cloudshell-launcer.ps1"

Now right click on shortcut and run (as “Admin” if required)

To connect azure account, use Device Authentication: DeviceAuthentication - Connect Azure Account in CloudShell when MFA is Enabled

Chocholatey

Install chocolatey using PowerShell

#
# PowerShell console
#
Set-ExecutionPolicy Bypass -Scope Process -Force; `
  iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
#Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

#
# PowerShell ISE
#
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

Upgrade Chocholatey

choco upgrade chocolatey

Check Chocholatey version

choco upgrade pkgname --noop

Pulumi

Install pulumi using chocolatey

choco install pulumi -y

Check version

pulumi version

Cmder

Install cmder using chocolatey

choco install cmder -y
# choco upgrade cmder

Open Cmder

Kubectl

Install kubectl using chocolatey

choco install kubernetes-cli -y
# choco upgrade kubernetes-cli

Check version

kubectl version --client

Helm

Install Helm using chocolatey

choco install kubernetes-helm -y

Upgrade Helm

choco upgrade kubernetes-helm -f -y

Add Helm to ‘Path’ environment variable

Check if Helm was istalled successfully (open PowerShell or cmd)

# you might need to restart your cmd/Cmder/Git Bash/PowerShell
helm version

Connect Cmder to Azure

Login to Azure

az aks login

Using Cmder to Interact with AKS Cluster

# if you are not logged in yet
az aks login

# get aks credential
az aks get-credentials --resource-group xyz-rg --name xyz-aks-cluster

AKS cluster Access Control

Once authenticated into AKS cluster, now execute kubectl commands

# make sure you set the correct namespace
kubectl config view --minify | grep namespace  # check namespace, for default namespace no value (empty) might be shown
kubectl config set-context --current --namespace xyz # set namespace

# Nodes
kubectl get nodes

# Pods
kubectl get pods

# Services
kubectl get svc

Installing NodeJS and NPM

Prerequisites

Install NodeJS using chocolatey

choco install -y --force nodejs

Check version

node -v 

NPM

# install
npm i -g npm

# check version
npm -v

Installing Angular CLI

Prerequisites

Installing Angular CLI using npm

#
# Global installation
#
npm install -g @angular/cli@
npm install -g @angular/cli@latest

#
# Local installation -> don't use '-g' flag
#
npm install @angular/cli@latest

#
# install specific (target) version
# 
npm install -g @angular/cli@11.2.11

Check version

ng version

Uninstall Angular CLI

npm uninstall -g @angular/cli
npm uninstall @angular/cli
npm cache clean --force

#
# If you installed specific version
# i.e. version 11.2.11
#
npm uninstall -g @angular/cli@11.2.11
npm uninstall @angular/cli@11.2.11
npm cache clean --force

Installing jq

Make sure chocolatey is installed (see above)

choco install jq -y
# flag '-y' for accepting all