PS: Customize Your Prompt

2 minute read

Description:

Follow this post to setup a customized PS Prompt. Most of this comes from this post.

To Resolve:

  1. Open admin PS and type:
# Set your PowerShell execution policy
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force

# Install Chocolatey
iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex

# Install Chocolatey packages
choco install vim -y
choco install conemu -y

# Install PowerShell modules
Install-PackageProvider NuGet -MinimumVersion '2.8.5.201' -Force
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
  1. Close out of the PS prompt and open ConEmu

#configure it like so:
Settings – Appearance: Single instance mode
Quake Style = Check the slide down checkbox
Startup = Change dropdown to {Shells:Powershell}
Keys and Macro = Assign Ctrl+` #mine was already configured
Colors = Import a theme from here or customize your own.

That’s it! Make sure to add the following to your profile:

Set-Alias vim -Value “C:\Program Files (x86)\vim\vim80\gvim.exe”

Import-Module PSColor

As an addition to this, I like to set my prompt like so..

function Prompt {
    $identity = [Security.Principal.WindowsIdentity]::GetCurrent()
    $principal = [Security.Principal.WindowsPrincipal] $identity
    $(if (test-path variable:/PSDebugContext) { '[DBG]: ' }
    elseif($principal.IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
        {
        Write-Host ("[ADMIN] ") -nonewline -foregroundcolor Green
        # Print the current time:
        Write-Host ("[") -nonewline -foregroundcolor DarkGray
        Write-Host (Get-Date -format 'g') -nonewline -foregroundcolor Gray
        Write-Host ("][") -nonewline -foregroundcolor DarkGray
        # Print the working directory:
        Write-Host ($PWD) -nonewline -foregroundcolor Gray
        Write-Host ("]") -nonewline -foregroundcolor DarkGray
        Write-Host ("`r`n") -nonewline -foregroundcolor DarkGray
        # Print the prompt symbol:
        Write-Host ("#") -nonewline -foregroundcolor Green
        return " ";
        }
    else{
        # Print the current time:
        Write-Host ("[") -nonewline -foregroundcolor DarkGray
        Write-Host (Get-Date -format 'g') -nonewline -foregroundcolor Gray
        Write-Host ("][") -nonewline -foregroundcolor DarkGray
        # Print the working directory:
        Write-Host ($PWD) -nonewline -foregroundcolor Gray
        Write-Host ("]") -nonewline -foregroundcolor DarkGray
        Write-Host ("`r`n") -nonewline -foregroundcolor DarkGray
        # Print the prompt symbol:
        Write-Host ("#") -nonewline -foregroundcolor Green
        return " ";
        }
    )}

Copy and paste this into your profile. What it will do is make each command in Powershell or Powershell ISE have it’s own line.

A regular prompt will look like: [Date / Time] [Current Directory]

An Admin prompt will look the same but will have a green [ADMIN] in front.

More info on customizing your prompt can be found here.


Update 2018-03:

My current prompt looks like:

Function Prompt
{
    <# 
.Synopsis
Sets the prompt to one of three choices. See comments.
.Description
Sets the prompt to one of three choices. See comments.
.Notes
2018-01-02: Added Linux comment
2017-10-26: v1.0 Initial script
#>

    $CurPath = $ExecutionContext.SessionState.Path.CurrentLocation.Path
    If ($CurPath.ToLower().StartsWith($Home.ToLower()))
    {
        $CurPath = "~" + $CurPath.SubString($Home.Length)
    }

    $Date = (Get-Date -Format "yyyy-MM-dd@hh:mm:sstt")
    
    If (Test-IsAdmin)
    {
    # Option 1: Full brackets
	#Write-Host "[$(($env:USERNAME.ToLower()))@$(($env:COMPUTERNAME.ToLower()))][$Date][$CurPath]" -NoNewLine 
    #"$('>' * ($nestedPromptLevel + 1)) "
	#Return " "
    
	# Option 2: For a more Linux feel...
    Write-Host "$(($env:USERNAME.ToLower()))" -ForegroundColor Cyan -NoNewLine
	Write-Host "@" -ForegroundColor Gray -NoNewLine
	Write-Host "$(($env:COMPUTERNAME.ToLower()))" -ForegroundColor Red -NoNewLine
	Write-Host ":$curPath#" -ForegroundColor Gray -NoNewLine
    Return " "
	
	# Option 3: For a minimalistic feel
	#$promptString = "[$curPath]>"
	#Write-Host $promptString -NoNewline
    #Return " "
	}
    Else
    {
    # Option 1: Full brackets
	#Write-Host "[$(($env:USERNAME.ToLower()))@$(($env:COMPUTERNAME.ToLower()))][$Date][$CurPath]" -NoNewLine 
    #"$('>' * ($nestedPromptLevel + 1)) "
	#Return " "
    
	# Option 2: For a more Linux feel...
    Write-Host "$(($env:USERNAME.ToLower()))" -ForegroundColor Cyan -NoNewLine
	Write-Host "@" -ForegroundColor Gray -NoNewLine
	Write-Host "$(($env:COMPUTERNAME.ToLower()))" -ForegroundColor Red -NoNewLine
	Write-Host ":$curPath$" -ForegroundColor Gray -NoNewLine
    Return " "
	
	# Option 3: For a minimalistic feel
	#$promptString = "[$curPath]>"
	#Write-Host $promptString -NoNewline
    #Return " "
    }
}