Tagged: Prompt

Nice PowerShell prompt

Been playing around with PowerShell the last days, and with nothing else better to do I did a nice looking prompt. 🙂

This prompt will give you a marker if you run it as Administrator and another mark if you are outside the filesystem.

How to:
First you need to edit your profile so it autoload the new prompt.

notepad $profile

Then paste this code, save and start a new PowerShell command line.

function prompt
{
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	#                                                                              Rikard Ronnkvist / snowland.se
	# Multicolored prompt with marker for windows started as Admin and marker for providers outside filesystem
	# Examples
	#    C:\Windows\System32>
	#    [Admin] C:\Windows\System32>
	#    [Registry] HKLM:\SOFTWARE\Microsoft\Windows>
	#    [Admin] [Registry] HKLM:\SOFTWARE\Microsoft\Windows>
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	# New nice WindowTitle
	$Host.UI.RawUI.WindowTitle = "PowerShell v" + (get-host).Version.Major + "." + (get-host).Version.Minor + " (" + $pwd.Provider.Name + ") " + $pwd.Path

	# Admin ?
	if( (
		New-Object Security.Principal.WindowsPrincipal (
			[Security.Principal.WindowsIdentity]::GetCurrent())
		).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
	{
		# Admin-mark in WindowTitle
		$Host.UI.RawUI.WindowTitle = "[Admin] " + $Host.UI.RawUI.WindowTitle

		# Admin-mark on prompt
		Write-Host "[" -nonewline -foregroundcolor DarkGray
		Write-Host "Admin" -nonewline -foregroundcolor Red
		Write-Host "] " -nonewline -foregroundcolor DarkGray
	}

	# Show providername if you are outside FileSystem
	if ($pwd.Provider.Name -ne "FileSystem") {
		Write-Host "[" -nonewline -foregroundcolor DarkGray
		Write-Host $pwd.Provider.Name -nonewline -foregroundcolor Gray
		Write-Host "] " -nonewline -foregroundcolor DarkGray
	}

	# Split path and write \ in a gray
	$pwd.Path.Split("\") | foreach {
	    Write-Host $_ -nonewline -foregroundcolor Yellow
	    Write-Host "\" -nonewline -foregroundcolor Gray
	}

	# Backspace last \ and write >
	Write-Host "`b>" -nonewline -foregroundcolor Gray

    return " "
}