I'm using PowerShell on Windows 7. How can I configure PowerShell so that it only displays the current folder name (instead of the full path) in the shell prompt?
For example, instead of C:\folder\directory\name>
, I want name>
.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityYou 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
[System.IO.Directory]::GetCurrentDirectory()
does not represent powershell's current location, but rather the process working directory, which are not always the same. Replace with Get-Location
GetCurrentDirectory
. Remember that PowerShell does not always mean the file system. Using the registry snap-in, I could use cd hkcu:
and now the prompt would be totally off.
$PWD
instead of calling Get-Location
. E.g. function prompt { (Split-Path -Leaf $pwd) + '> ' }
prompt
in your profile file. It just has to be in the global session scope. The default seems equivalent to function prompt {"PS " + (Get-Location) + ">"}
, while function prompt {""}
gives the same as function prompt {"PS>"}
. Use function prompt {" `b"}
to get an empty prompt (at least visually...).
Dec 17, 2017 at 10:31
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 )>" }
\\myserver\my\path
.
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.
$Env:userprofile
is equivalent to echo %userprofile%
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.
I wanted a more elaborate prompt and constructed a new prompt. This answer is not meant to replace the current answer, but more as an addition for those who stumble upon this later. This prompt will have full support over network paths and looks very elegant.
function Prompt
{
write-host "PS " -ForegroundColor Magenta -NoNewline
write-host (get-date -Format "yyyy-MM-dd HH:mm:ss") -ForegroundColor Yellow -NoNewline
write-host " | " -ForegroundColor DarkGray -NoNewline
write-host "\\$env:COMPUTERNAME " -NoNewline
write-host " | " -ForegroundColor DarkGray -NoNewline
if( (Get-Location).Drive -ne $null)
{
write-host (Get-Location)
$networkdrive = $false
}
else
{
$networkdrive = $true
$first, $second, $third, $folder = (Get-Location).path.Split("\")
write-host "\" -NoNewline
$folder | foreach-object {
write-host "\$_" -NoNewline
}
write-host
}
write-host "PS " -ForegroundColor Magenta -NoNewline
if( (Get-Location).Drive -ne $null)
{
write-host "$((Get-Location).Drive):\" -NoNewline
}
else
{
write-host "\\$((Get-Location).path.Split("\")[3])\" -NoNewline
}
if( (Get-Location).path.Split("\").Count -gt 2 -and $networkdrive -eq $false)
{
write-host "…\" -NoNewline
}
if( (Get-Location).path.Split("\").Count -gt 5 -and $networkdrive -eq $true)
{
write-host "…\" -NoNewline
} write-host "$((Get-Location).path.Split("\")[-1])" -NoNewline
write-host ">" -NoNewline
return " "
}
It will show a prompt like this:
PS 2021-03-24 02:03:39 | \\MYPC | \\Server\Share\Folder1\Folder2
PS \\Server\…\Folder2> cd c:\Users\User
PS 2021-03-24 02:03:39 | \\MYPC | C:\Users\User
PS C:\…\User>
The below single line works fine for me:
(Get-Location | Get-Item).Name
For posh-git the best way I've found is to customize the $GitPromptSettings.DefaultPromptPath.Text
property.
In the profile:
$GitPromptSettings.DefaultPromptPath.Text = '$(Split-Path -leaf -path ($ExecutionContext.SessionState.Path.CurrentLocation))'
Results in this (can't embed images yet, seemingly):
I created this neat prompt to that shows the drive and last folder.
For you example it would render as
PS C:\Users\b.HQ\Desktop\tsdev\my_folder>
as
PS C:\...\my_folder>
The prompt function is:
function prompt {"PS " + (get-location).drive.name+":\...\"+ $( ( get-item $pwd ).Name ) +">"}
If you are using anaconda then you probably want to change your profile function to
function Prompt {
$p = Split-Path -leaf -path (Get-Location)
"$env:CONDA_PROMPT_MODIFIER $p> "
}
Although I think that just splitting on the last path component is a bit too much. The problem that I'm trying to solve is paths that take up an entire line (actually quite common in a codebase)
function Prompt {
$maxLength = 50
$p = (Get-Location).Path
if ($p.Length -gt $maxLength) {
$s = Split-Path -leaf -path (Get-Location)
if ($s.Length -gt $maxLength - 1) {
$p = $s
}
else {
$p = "...$($p.Substring($p.Length - $maxLength + 3))"
}
}
"$env:CONDA_PROMPT_MODIFIER $p> "
}
You can ditch the $env:CONDA_PROMPT_MODIFIER
if you don't use Anaconda
I write a function that provides three styles {None
, Name
, Fulllpath
} so you can easily change the prompt anytime.
<#
.Description
It can simplify the prompt, so if your path is too long still okay.
Implement: You are only to modify the "Prompt" function then done.
.Parameter style
You can use tab to select the style.
- None: do not show any text except ">" (this is default options)
- Name: show the basename only
- Fullpath: show the full path
.Outputs
The output type is "string" so you must use "Invoke-Expression" to make it effective.
.Example
Set-Prompt | iex
Set-Prompt | Invoke-Expression
.Example
Set-Prompt -style Name | iex
Set-Prompt -style Fullpath | iex
.Example
Set-Prompt -Verbose
#>
function Set-Prompt {
param (
[Parameter()]
[ValidateSet('None', 'Name', 'Fullpath')]
[string]$style = "None"
)
[string]$myPromptFunc = ""
switch ($style) {
"None" {
$myPromptFunc = 'function Prompt { ">" }'
}
"Name" {
$myPromptFunc = @"
function Prompt {
"`$((Get-Item `$pwd).Name)>"
}
"@
}
"Fullpath" {
$myPromptFunc = 'function Prompt { Write-Output "$($pwd.Path)>" }'
}
}
if ($VerbosePreference -eq "Continue") {
Write-Host "Call " -NoNewLine
Write-Host 'Set-Prompt | Invoke-Expression' -ForegroundColor Yellow -NoNewLine
Write-Host " to apply."
Write-Host "The prompt function will be changed as follow" -ForegroundColor Green
}
Write-Output $myPromptFunc
}
demo
# Example1 default: None
PS C:\Windows\System32> Set-Prompt | iex
>
# Example1 Name
PS C:\Windows\System32> Set-Prompt -style Name | iex
System32>cd ..
Windows>
# Example3 fullpath
PS C:\Windows\System32> Set-Prompt -style Fullpath | iex
C:\Windows\System32>cd ..
C:\Windows>
You don't have to copy-paste every time.
You can create a source directory try to search psd1
, psm1
and then add the path to PSModulePath
, and you can use it directly next time.
for powershell5, you may need to
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -F
before you use the command.For PowerShell 7, you can use it directly.
function prompt { "[$((''+$PWD).replace($HOME, '~'))]> " }