1

When you work in the Windows terminal, you see your full current fold path on the left. I would like to make it looking like in Linux where only the last directory name is shown, becasuse my current working folds are too deep & paths demands more than a half of screen. I tried to google this through a lot of phrases but it led to nothing. I suppose the problem is in the mismatch of my English lexicon with a native user's.

windows show the last path as in linux, windows not show whole path, powershell show not all directory path &c. gave me nothing useful or relevant. Is there a way to set cmd to suppress showing the full path?

1

2 Answers 2

3

In PowerShell, Prompt is by default, a built-in function that returns a string. In a PowerShell session where the prompt hasn't been customized, the underlying code can be viewed by typing:

  • (Get-Command prompt).ScriptBLock

     PS C:\>
     >> "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel +
     >> 1)) ";
     >> # .Link
     >> # https://go.microsoft.com/fwlink/?LinkID=225750
     >> # .ExternalHelp System.Management.Automation.dll-help.xml
     >>
     >>
    

You can use this code as the basis for a custom function that will override the default.

In the above, the path is obtained by this code:

  • $($executionContext.SessionState.Path.CurrentLocation)

To pare it down to the current directory name, change it to:

  • $($executionContext.SessionState.Path.CurrentLocation.Path.Split('\')[-1])

So to override the default prompt, declare a function of the same mane:

function Prompt {
    "PS $($executionContext.SessionState.Path.CurrentLocation.Path.Split('\')[-1])$('>' * ($nestedPromptLevel +1)) "
}

To make the custom prompt the default for new sessions, add the function definition to your PowerShell profile. The following code will add it to your existing profile, or create your profile if you don't have one.

If (!(Test-Path -Path $PROFILE )) {
    New-Item -Type File -Path $PROFILE -Force | out-null }
@'
function Prompt {
    "PS $($executionContext.SessionState.Path.CurrentLocation.Path.Split('\')[-1])$('>' * ($nestedPromptLevel +1)) "
}
'@ | Add-Content -Path $PROFILE -Force
0

The command prompt sets the starting text of the windows terminal. Its options are shown here:

Zeichen Beschreibung
$q = (Equal sign)
$$ $ (Dollar sign)
$t Current time
$d Current date
$p Current drive and path
$v Windows version number
$n Current drive
$g > (Greater than sign)
$l < (Less than sign)
$b | (Pipe symbol)
$_ ENTER-LINEFEED
$e ANSI escape code (code 27)
$h Backspace (to delete a character that has been written to the command line)
$a & (Ampersand)
$c ( (Left parenthesis)
$f ) (Right parenthesis)
$s Space

From: https://docs.microsoft.com/de-de/windows-server/administration/windows-commands/prompt

As you can see it does not list any option to set the start text to the current folder. To get more space you can change it to only show something like ">" with:

prompt $G

This was already answered in: here

3
  • Thank you! I did not know that this is called prompt. I prefer to avoid workarounds with prompt command, because this does not look trusty. I thought that this is somewhere in settings.
    – Vovin
    Jul 1, 2022 at 15:56
  • Please could you edit and quote the relevant part of the link in your answer, in case of link-rot. Otherwise your answer is almost a link-only answer. Jul 1, 2022 at 16:34
  • You are right. I expanded the answer.
    – GURKE
    Jul 1, 2022 at 16:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .