Configuring PowerShell (shell prompt)

4 min read
May 5, 2023

In Windows PowerShell, customizing your shell prompt can enhance your development environment by providing valuable information at a glance. This guide walks you through configuring Windows PowerShell to display a customized prompt with the username, date, and current folder name.

Checking for Existing Profile

STEP 1: Before proceeding, check if you have an existing profile set up in PowerShell:

test-path $profile

STEP 2: If the result is false, create a new profile:

new-item -path $profile -itemtype file -force

STEP 3: Now, open the profile in Notepad:

notepad $profile

STEP 4: Paste the following function to display the current folder name as the prompt:

function prompt {
  $p = Split-Path -leaf -path (Get-Location)
  "$p> "
}

STEP 5: Save the file. The location is predetermined, so you shouldn't need to choose one.

Reload Visual Studio Code; you may encounter a message about running scripts. Alternatively, execute the following command before reloading:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Reload Visual Studio Code once more.

Examples for Terminal Short Paths

  • Follow STEP 3 again, paste the source code, save the file, and reload Visual Studio Code.

Sample Example 1

function prompt {
  $time = Get-Date -Format "HH:mm:ss"
  $location = "$($time | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor DarkYellow }) $(" | " | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor Gray }) $($((Get-Location).Drive.Name + ": " + $(Get-Item $pwd).Name) | ForEach-Object { Write-Host $_  -ForegroundColor Cyan }) $(">" | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor Magenta })"
  return " "
}

Sample Example 2

function prompt {
  $time = Get-Date -Format "HH:mm:ss"
  $location = "$($time | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor DarkYellow }) $(" | " | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor Gray }) $($((Get-Location).Drive.Name + ": " + $(Get-Item $pwd).Name) | ForEach-Object { Write-Host $_  -ForegroundColor Cyan }) $("$" | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor Magenta })"
  return " "
}

Sample Example 3

function prompt {
  $username = $env:USERNAME.ToLower()
  $deviceName = $env:COMPUTERNAME
  $location = "$($username | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor DarkYellow }) $("@" | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor Gray }) $($deviceName | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor DarkYellow }) $(" | " | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor Gray }) $($((Get-Location).Drive.Name + ": " + $(Get-Item $pwd).Name) | ForEach-Object { Write-Host $_  -ForegroundColor Cyan }) $(">" | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor Magenta })"
  return " "
}

Sample Example 4

function prompt {
  $username = $env:USERNAME.ToLower()
  $deviceName = $env:COMPUTERNAME
  $time = Get-Date -Format "HH:mm:ss"
  $location = "$($username | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor DarkYellow }) $("@" | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor Gray }) $($deviceName | ForEach-Object { Write-Host $_ $time -NoNewline -ForegroundColor DarkYellow }) $(" | " | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor Gray }) $($((Get-Location).Drive.Name + ": " + $(Get-Item $pwd).Name) | ForEach-Object { Write-Host $_ -ForegroundColor Cyan }) $(">" | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor Magenta })"
  return " "
}

Sample Example 5

function prompt {
  $username = "herogautam"
  $time = Get-Date -Format "dd-MM-yyyy HH:mm:ss"
  $location = "$($username | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor Magenta }) $(" |" | ForEach-Object { Write-Host $_ $time -NoNewline -ForegroundColor DarkYellow }) $(" | " | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor Gray }) $($((Get-Location).Drive.Name + ": " + $(Get-Item $pwd).Name) | ForEach-Object { Write-Host $_ -ForegroundColor Cyan }) $(">" | ForEach-Object { Write-Host $_ -NoNewline -ForegroundColor Magenta })"
  return " "
}

Sample Example 6

function Prompt {
  write-host "PS " -ForegroundColor Magenta -NoNewline
  write-host (get-date -Format "dd-MM-yyyy HH:mm:ss") -ForegroundColor Yellow -NoNewline
  write-host " | " -ForegroundColor DarkGray -NoNewline
  write-host "\\$env:COMPUTERNAME " -ForegroundColor Gray -NoNewline
  write-host " | " -ForegroundColor DarkGray -NoNewline
  if ((Get-Location).Drive -ne $null) {
    write-host (Get-Location) -ForegroundColor Cyan
    $networkdrive = $false
  } else {
    $networkdrive = $true
    $first, $second, $third, $folder = (Get-Location).path.Split("\")
    write-host "\" -NoNewline
    $folder | foreach-object {
      write-host "\$_" -NoNewline
    }
    write-host -ForegroundColor Cyan
  }
  write-host ">" -ForegroundColor Magenta -NoNewline
  return " "
}

List of Powershell colors

ColorHTML Color Code
DarkCyan#00cdcd
DarkRed#cd0000
Magenta#ff00ff
Blue#5c5cff
DarkGray#7f7f7f
DarkYellow#cdcd00
Red#ff0000
Cyan#00ffff
DarkGreen#00cd00
Gray#e5e5e5
White#ffffff
DarkBlue#6495ed
DarkMagenta#cd00cd
Green#00ff00
Yellow#ffff00
Explore Related Topics
Wanna Discover more Interesting Topics?
markdown
Apr 9, 2022

Markdown Syntax Guide

Markdown is a lightweight markup language that allows you to format text in a plaintext editor while still having a structured and readable output. It is often used for writing documentation, readme files, blog posts, and other content where readability and simplicity are important. Markdown syntax is simple and intuitive, making it an ideal choice for writing and formatting text without the need for complex HTML tags. Its flexibility and ease of use make it a popular tool among developers, writers, and content creators alike. By using basic symbols and characters, you can structure documents that are both humanreadable and machinereadable, w
nextjs
Dec 21, 2024

Next.js (Pages Router) and Contentlayer Setup

In this blog we will look how to setup Next.js and Contentlayer for an Blog page. Step one Install necessary deps bash npm install \ contentlayer nextcontentlayer \ shiki rehypeshiki rehypeprettycode \ rehypeautolinkheadings rehypeslug rehyperewrite rehypestringify \ remarkgfm unified \ datefns readingtime create contentlayer.config.ts file add contentlayer.config.ts in your root of the project and you have to create your blog files in this folder folder strcture of the necessary files and folders bash /public /content /blog /markdownsyntaxguide index.mdx contentlayer.config.ts tsconfig.json nextjs.config.ts ts import { defineDocumentType, ma
nextjs
Dec 21, 2024

Next.js (Pages Router) and Contentlayer Setup

Building a Scalable MDX Blog with Next.js and Contentlayer As engineering leads, one of our recurring challenges is balancing scalability, readability, and extensibility when building contentheavy frontend applications. A lightweight, composable blog system using Next.js (Pages Router) and Contentlayer checks all the right boxes—type safety, statically generated performance, and full MDX control. In this post, you'll set up a productiongrade foundation for a statically rendered blog using Contentlayer and Next.js, with an emphasis on correctness, longterm maintainability, and modern DX. Project Structure Here’s the minimal directory layout yo