Tag Archives: PowerShell

PowerShell in a container

Recently I found that we can execute a PowerShell script in a docker container. There is a base image for PowerShell in docker hub and you can use that to create your own image with your PowerShell script. Now when you create docker image and container from that, your PowerShell script can execute inside that container.

Example: https://github.com/Arnab-Developer/PowerShellInContainer

Custom PowerShell prompt

My custom PowerShell prompt without the use of oh-my-posh. Create or open file with name Microsoft.PowerShell_profile.ps1 at location C:\Users\<your user name>\Documents\PowerShell\ and paste this code inside that. Then open a PowerShell window to check the prompt.

function prompt
{
    [string] $currentFolderName = GetCurrentFolderName
    [string] $gitStatus = (git status)
    if ([string]::IsNullOrEmpty($gitStatus))
    {
        Write-Host "→ $currentFolderName" -NoNewLine -ForegroundColor DarkCyan
    }
    else
    {
        Write-Host "→ $currentFolderName (" -NoNewLine -ForegroundColor DarkCyan
        Write-Host "$(git branch --show-current)" -NoNewLine -ForegroundColor DarkRed
        Write-Host ")" -NoNewLine -ForegroundColor DarkCyan
    }
    return " "
}

function GetCurrentFolderName
{
    [string] $location = Get-Location
    [string] $currentFolderName = [string]::Empty
    if ($location.EndsWith("\"))
    {
        $currentFolderName = $location
    }
    else
    {
        [int] $lastIndexOfSlash = $location.LastIndexOf("\")
        $currentFolderName = $location.SubString($lastIndexOfSlash + 1)
    }
    return $currentFolderName
}

PowerShell for .NET developer

I needed to create a script to automate some steps in Azure. I had mainly two options. Azure CLI and Azure PowerShell. CLI is a good option to do quick things on Azure because it’s easy to write and remember syntax. But when I want to create a script, PowerShell is the option for me because I am a .NET developer. PowerShell’s syntax and concept is more familiar to a .NET developer like me.

I can apply almost same object oriented programming concepts in PowerShell like what I used to do in C#. Ok, not all object oriented concept but at least I can break my solution into different classes with single responsibility. I can create properties and methods inside them. Create objects of that class and use the properties and methods.

GitHub – Arnab-Developer/PowerShellClassDemo: PowerShell class demo

Every PowerShell cmdlet returns some object you can store them in a variable and use the properties of them. You can use collection of object. You can use the .NET Framework level APIs in PowerShell.

Overall if you are a .NET developer and you need to automate some administrative work, PowerShell can be a very good choice for you. Syntax is different but the concept and the Framework level APIs are same like a .NET program.

about Classes – PowerShell | Microsoft Docs