Wongesse
Wongesse

Reputation: 112

Change Windows command prompt to show only current folder

Instead of showing

C:\Users\test_user\Documents\Folder\etc

show

\etc

or if possible limit it to a certain number

\Document\Folder\etc

Upvotes: 7

Views: 6152

Answers (5)

Here is a .ps1 file i use to do this for myself.

<#
FileName: promptPsShort.ps1

To set the prompt to the last folder name in the path:
> function prompt {$l=Get-Location; $p="$l".split("\")[-1]; "PS $p> "}  
  # works at cmd prompt, BUT NOT DIREECTLY from a .ps1 file.

RESEARCH

1. google: powershell 7 copy text into clipboard
  [How to copy text from PowerShell](https://superuser.com/q/302032/236556)
  [Set-Clipboard](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/?view=powershell-7)
2. google: powershell escape double quote
  [Escaping in PowerShell](http://www.rlmueller.net/PowerShellEscape.htm)
3. google: powershell raw string
  [About Quoting Rules](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7)

4. Usage example: powershell
PS C:\flutter_beta\flutter\examples\catalog\android\app\src\main> pwd

Path
----
C:\flutter_beta\flutter\examples\catalog\android\app\src\main

PS C:\flutter_beta\flutter\examples\catalog\android\app\src\main> promptPsShort.ps1
Paste the current Clipboard contents into the Powershell Command Line and press Enter.
PS C:\flutter_beta\flutter\examples\catalog\android\app\src\main> function prompt {$l=Get-Location; $p="$l".split("\")[-1]; "PS $p> "}
PS main>
PS main>
PS main>

#>


$shortPromptCmdStr = @'
function prompt {$l=Get-Location; $p="$l".split("\")[-1]; "PS $p> "}
'@

Set-Clipboard -Value $shortPromptCmdStr

write-host "Paste the current Clipboard contents into the Powershell Command Line and press Enter."

Love and peace, Joe

Upvotes: -1

Bobby Wayne
Bobby Wayne

Reputation: 11

The following is a simple batch script which can set the prompt to include only the current folder. Note that it does not work on directory names with certain characters such as parenthesis and spaces. I named it cdd.bat.

@echo off
cd %1

for %%i in (%CD%) do set NEWDIR=%%~ni
PROMPT %NEWDIR%$G

Upvotes: 1

Suraj Donthi
Suraj Donthi

Reputation: 329

Like others pointed out, you can use the command - prompt to set the text that is shown in cmd.

While you cannot dynamically set the path to just the parent folder, you can manually set it using:

prompt {text}

So in your case, you can set it as:

prompt etc\$G

This will result in:

etc\>

$G adds an arrow sign. You can refer the documentation for detailed explanation.

Upvotes: -1

Chandre Gowda
Chandre Gowda

Reputation: 930

If you check in help prompt /? there are two options that can either show the current drive or full path.

I would suggest to use new line option along with the Drive so that you will get more space to view/type the command using below combination.

prompt $P$_$G

With this you will be able to see the Path in the line above the prompt.

Upvotes: 8

Gary Batterbee
Gary Batterbee

Reputation: 307

In short, can't see a simple way of doing it. In order to change the prompt options you can use the prompt command. The configuration you're looking for isn't listed. The available options can be viewed by prompt /? in the command window.

https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/prompt.mspx?mfr=true

Upvotes: 0

Related Questions