Configuring PowerShell (shell prompt)

6 min read
Nov 12, 2022

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.

Text Encryption Tool Interface

Text Encryption Tool Interface

Checking for Existing Profile

STEP 1: Check for an Existing Profile

Run the following command to check if you have an existing profile set up in PowerShell:

test-path $profile

STEP 2: Create a New Profile (if needed)

If the result is false, create a new profile:

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

STEP 3: Open the Profile in Notepad

Open the profile for editing:

notepad $profile

STEP 4: Add a Basic Prompt Function

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

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

Text Encryption Tool Interface

STEP 5: Save and Reload PowerShell

Save the file. If you encounter a script execution warning, run the following command:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Reload PowerShell or Visual Studio Code.


Examples for Terminal Short Paths

Example 1: Prompt with > and Date

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 " "
}

Example 2: Prompt with $ and Date

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 " "
}

Example 3: Prompt with > and username@SYSTEM

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 " "
}

Example 4: Prompt with >, username@SYSTEM, and Date

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 $_ -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 " "
}
Text Encryption Tool Interface

Text Encryption Tool Interface

Example 5: Prompt with >, username, and Full Date-Time

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 " "
}

Example 6: Prompt with >, username, Date, and Current Directory

powershellExample 6
Copy code
function Prompt1 {
  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
  } else {
    write-host (Get-Location).Path -ForegroundColor Cyan
  }
  write-host ">" -ForegroundColor Magenta -NoNewline
  return " "
}
powershellExample 6
Copy code
const [age, setAge] = useState(50);
const [name, setName] = useState("Taylor");
Hello
// Using 'typeof' to infer types
const person = { name: "Alice", age: 30 };
type PersonType = typeof person;  // { name: string; age: number }

// 'satisfies' to ensure a type matches but allows more specific types
type Animal = { name: string };
const dog = { name: "Buddy", breed: "Golden Retriever" } satisfies Animal;

// Generics with 'extends' and default values
function identity<T extends number | string = string>(arg: T): T {
  return arg;
}

let str = identity();  // Default type is string
let num = identity(42);  // T inferred as number

// 'extends' with interface and class
interface HasLength {
  length: number;
}

function logLength<T extends HasLength = string>(arg: T): void {
  console.log(arg.length);
}

logLength("Hello");    // OK: string has length (default is string)
logLength([1, 2, 3]);  // OK: array has length
// logLength(123);      // Error: number doesn't have length

// 'typeof' with functions
function add(x: number, y: number): number {
  return x + y;
}
 / sdf
Text Encryption Tool Interface

Text Encryption Tool Interface


PowerShell Colors Reference

ColorHTML Code
Dark Cyan#00cdcd
Dark Red#cd0000
Magenta#ff00ff
Blue#5c5cff
Dark Gray#7f7f7f
Dark Yellow#cdcd00
Red#ff0000
Cyan#00ffff
Dark Green#00cd00
Gray#e5e5e5
White#ffffff
Dark Blue#6495ed
Dark Magenta#cd00cd
Green#00ff00
Yellow#ffff00
Wanna Discover more Interesting Topics?
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
powershell
May 5, 2023

Configuring PowerShell (shell prompt)

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: powershell testpath $profile STEP 2: If the result is false, create a new profile: powershell newitem path $profile itemtype file force STEP 3: Now, open the profile in Notepad: powershell notepad $profile STEP 4: Paste the following function to displ