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
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s