Configure Windows PowerShell to display only the current folder name in the shell prompt
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzling Curiosities
--
Chapters
00:00 Configure Windows Powershell To Display Only The Current Folder Name In The Shell Prompt
00:26 Accepted Answer Score 112
01:36 Answer 2 Score 11
02:27 Answer 3 Score 17
02:48 Answer 4 Score 5
03:48 Thank you
--
Full question
https://superuser.com/questions/446827/c...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#windows7 #powershell
#avk47
ACCEPTED ANSWER
Score 112
You have to customize the prompt function in your PowerShell profile (%userprofile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
); it may be blank or even not exist if you have never modified it before.
Open your profile (e.g., open the aforementioned file or while in PowerShell,
Notepad $profile
)Add the following to your profile:
function prompt { $p = Split-Path -leaf -path (Get-Location) "$p> " }
Save the profile
Restart PowerShell
Optional. If you get a message that says you are not allowed to run scripts, then you need to copy/paste this line in PowerShell:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
and restart.
Windows PowerShell execution policies let you determine the conditions under which Windows PowerShell loads configuration files and runs scripts.
You can set an execution policy for the local computer, for the current user, or for a particular session. You can also use a Group Policy setting to set execution policy for computers and users.
Source: Microsoft Documentation
ANSWER 2
Score 17
Change the prompt to show current folder without full path and greater than symbol at the end:
One way could be:
Function Prompt { "$( ( get-item $pwd ).Name )>" }
Or:
Function Prompt { "$( Split-Path -leaf -path (Get-Location) )>" }
Or:
Function Prompt { "$( ( Get-Location | Get-Item ).Name )>" }
ANSWER 3
Score 11
As an additional note, I couldn't do Synetech's command until I first created the $profile.
Open PowerShell
Type
$profile
and hit enter. This will display the profile path PowerShell relies on, even if it doesn't exist (it didn't for me). My path was different than what Synetech posted.>$profile C:\Users\[username]\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
I had to create both the WindowsPowerShell folder and the Microsoft.PowerShell_profile.ps1 file.
Add Synetech's code and restart PowerShell.
Note:
If you're using posh-git (which is installed when using GitHub desktop), Synetech's script will override the posh-git prompt. Additional prompt scripts for posh-git here.
ANSWER 4
Score 5
I found this was pretty easy - combining Synetech's answer and the information found at PowerShell profiles. Because I am a newbie to PowerShell. My steps (for Visual Studio Code):
test-path $profile
(in the PowerShell command prompt - is there a profile set up?)new-item -path $profile -itemtype file -force
(assuming the answer to the above is false)notepad $profile
(opens Notepad)Paste in
function prompt { $p = Split-Path -leaf -path (Get-Location) "$p> " }
Save (you shouldn't have to chose a location; it is already done for you)
Reload Visual Studio Code - you will probably get a message about running scripts (or just do next step before reload)
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
(at your PowerShell prompt, from the SuperĀ User answer)Reload Visual Studio Code
So it is mostly Synetech's answer with a bit to get started easily - especially step 2 makes this very easy.