Bon Leofen
Bon Leofen

Reputation: 85

how to get rid of present working directory in powershell

I am working on a project that is buried deep beneath the folders and directories and because of that my powershell's line is half filled with the path to the directory. I want to know if it is possible to get rid of the long string of directory path that is constantly showing.

Upvotes: 0

Views: 359

Answers (2)

Steven
Steven

Reputation: 7087

Building on the last answer I usually put the current path in the title bar of the console window and use the history ID for the prompt. Something like:

Function Prompt
{
    $cwd = (get-location).Path
    $LastHist = ( Get-History)[-1].Id + 1

    $Host.UI.RawUI.WindowTitle = "SPoSh_$($PSVersionTable.PSVersion.Major) - $cwd | User: $($env:USERNAME)"

    $Host.UI.Write("Green", $Host.UI.RawUI.BackGroundColor, "SPoSh_$($PSVersionTable.PSVersion.Major)")
    " #$LastHist : "
}

So this would look like:

"SPoSh_5 #1 : "

I leave a space at the end which makes it easier to double click select a previous command without capturing any of the prompt itself.

Note: If you are working in the regular console the title bar additions let you know where you are without crowding the prompt.

The title bar stuff doesn't show in some of the other non-colsole PowerShell hosts, like VSCode's integrated console, but during script dev the location is usually fairly static, so it's not too much trouble.

Upvotes: 1

shadow2020
shadow2020

Reputation: 1361

Add this to your script, then call the function after starting your script.

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

This way only the "leaf" folder will be shown for the prompt placeholder.

Upvotes: 1

Related Questions