Skip to content

Instantly share code, notes, and snippets.

@tats-u
Last active March 14, 2019 16:19
Show Gist options
  • Save tats-u/32cb142387c87ccc2a5114142d6d3e13 to your computer and use it in GitHub Desktop.
Save tats-u/32cb142387c87ccc2a5114142d6d3e13 to your computer and use it in GitHub Desktop.
Change the prompt of your PowerShell to one like Bash
function prompt() {
$return_code = $?
$domain_and_name = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$absolute = (Get-Location).Path
$esc = "$([char]27)"
$reset = $esc + "[0m"
$nas_prefix = 'Microsoft.PowerShell.Core\FileSystem::'
$is_root = (New-Object Security.Principal.WindowsPrincipal ([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
$prompt_sign = if ($is_root) { Write-Output '#' } else { Write-Output '$' }
return "${esc}[1;36mPS${reset}" `
+ " " `
+ "${esc}[1;32m$(Split-Path $domain_and_name -Leaf)" `
+ "@" `
+ "$(Split-Path $domain_and_name -Parent)${reset}" `
+ ":" `
+ "${esc}[1;34m$(if ($absolute.StartsWith($env:USERPROFILE)) { Write-Output $absolute.Replace($env:USERPROFILE, '~') } elseif ($absolute.StartsWith($nas_prefix)) { Write-Output $absolute.Replace($nas_prefix, '') } else { Write-Output $absolute })${reset}" `
+ "$(if($return_code) { Write-Output $prompt_sign } else { Write-Output "${esc}[1;31m${prompt_sign}${reset}" })" `
+ " "
}
@tats-u
Copy link
Author

tats-u commented Mar 14, 2019

Sample image:
sample image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment