PoshCode Archive  Artifact [71c4a7c004]

Artifact 71c4a7c0040f792c7e76832b0f5dc313c02cbefa6662afe5fff82e6423c560cb:

  • File Prompt-Replacement.ps1 — part of check-in [0bcf6441ab] at 2018-06-10 13:11:33 on branch trunk — Prompt replacement based on a script by iainsimpson (http://blogs.technet.com/iainsimpson/archive/2008/02/20/custom-powershell-prompt.aspx). (user: callias size: 2081)

# encoding: ascii
# api: powershell
# title: Prompt Replacement
# description: Prompt replacement based on a script by iainsimpson (http://blogs.technet.com/iainsimpson/archive/2008/02/20/custom-powershell-prompt.aspx).  
# version: 0.1
# type: function
# author: callias
# license: CC0
# x-poshcode-id: 2506
# x-archived: 2011-12-22T04:15:03
# x-published: 2011-02-15T12:44:00
#
# Add to $PROFILE to replace default prompt.  
# Displays the time, drive letter, and only the deepest directory.  Displays full path for UNC.  
# Color coded: local drives -> black on green,  removable drives -> black on yellow,  mapped drives -> black on magenta,  UNC paths -> white on blue,  all others (powershell drives and registry) -> black on red.
#
function prompt {
  $mapped_drives =    Get-WmiObject Win32_LogicalDisk -Filter "drivetype=4" | foreach {echo $_.deviceid}
  $local_drives =     Get-WmiObject Win32_LogicalDisk -Filter "drivetype=3" | foreach {echo $_.deviceid}
  $removable_drives = Get-WmiObject Win32_LogicalDisk -Filter "drivetype=2" | foreach {echo $_.deviceid}
  $t = $(get-date -format "HH:mm:ss")
  $a = (get-location).path
  $d = (get-location).path.substring(0,$a.indexof(":")+1)
  $a = $a.substring($a.LastIndexOf("`\")+1)
  if ((get-location).path.substring(0,(get-location).path.indexof(":")) -eq "Microsoft.PowerShell.Core\FileSystem") {
    $a = (get-location).path
	$a = $a.substring($a.indexof(":")+2)
    write-host -fore white -back blue "$t - $a ";"`$`> "}
  else {
    if ($a -eq "") {$a = "`\"}
    if ($d.length -gt 2) {
      write-host -ForegroundColor black -backgroundcolor red "[$t] - [$d] $a ";"`$`> "}
	elseif ($local_drives -contains "$d") {
      write-host -ForegroundColor black -backgroundcolor green "[$t] - [$d] $a ";"`$`> "}
	elseif ($removable_drives -contains "$d") {
      write-host -ForegroundColor black -backgroundcolor yellow "[$t] - [$d] $a ";"`$`> "}
	elseif ($mapped_drives -contains "$d") {
	  write-host -ForegroundColor black -backgroundcolor magenta "[$t] - [$d] $a ";"`$`> "}
  }
}