54

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>.

2
  • If you want the prompt with a path relative to your home directory, this worked for me: function prompt { "[$((''+$PWD).replace($HOME, '~'))]> " }
    – Chris
    Jun 6 '20 at 4:34
  • 1
    The question has already been answered, but I created a shortened Prompt for Poweshell that includes a bit more information. Has shortened names for some common directories (limited atm, may include more) and shortens home drive to "\": gist.github.com/1nVitr0/486b238a1e9a361dd0a2f6fc92f86d4f. Example: "$ ~\Drv\Docs\pers\proj\vsc\exts\daily-timelog >" Maybe it'll help somebody Jan 24 at 14:40
87

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.

  1. Open your profile (e.g., open the aforementioned file or while in PowerShell, Notepad $profile)

  2. Add the following to your profile:

    function prompt {
      $p = Split-Path -leaf -path (Get-Location)
      "$p> "
    }
    
  3. Save the profile

  4. 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

5
  • 1
    Not quite right. [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
    – zdan
    Jul 9 '12 at 20:57
  • +1, but I would use get-location instead of 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.
    – vcsjones
    Jul 9 '12 at 20:58
  • @Synetech It's buried quite deep in the formatting help :\
    – Bob
    Jul 9 '12 at 21:19
  • 2
    You can also use $PWD instead of calling Get-Location. E.g. function prompt { (Split-Path -Leaf $pwd) + '> ' }
    – Joey
    Nov 6 '13 at 11:43
  • 1
    You don't have to define 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...).
    – masterxilo
    Dec 17 '17 at 10:31
12

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 )>" }
3
  • 2
    The first and third are better than the accepted answer in that they work for network folders. Split-Path won't function correctly on a path like \\myserver\my\path.
    – Chris
    Oct 14 '19 at 13:32
  • And if I wanted to go back to before? How can I revert these?
    – Patrick
    Jul 27 '20 at 19:24
  • It depends on what you had before. If you want to show the path and then the "greater than" symbol, use something like this: Function Prompt { "$pwd>" }
    – Jelgab
    Aug 11 '20 at 12:04
10

As an additional note, I couldn't do Synetech's command until I first created the $profile.

  1. Open PowerShell

  2. 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
    
  3. I had to create both the WindowsPowerShell folder and the Microsoft.PowerShell_profile.ps1 file.

  4. 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.

4
  • 2
    It's the same. %userprofile% expands to C:\Users\[username]
    – DavidPostill
    Mar 15 '17 at 15:39
  • @DavidPostill that was my understanding, but my PowerShell didn't recognize %userprofile% as a directory. I don't believe my PowerShell is customized yet to recognize those paths, perhaps due to network settings?
    – Bryan
    Mar 15 '17 at 15:56
  • 2
    The syntax is different in PowerShell: $Env:userprofile is equivalent to echo %userprofile%
    – DavidPostill
    Mar 15 '17 at 16:37
  • $profile has a different value in ISE vs. the regular prompt, you may have to create 2 files.
    – Chris
    Oct 14 '19 at 13:36
3

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):

  1. test-path $profile (in the PowerShell command prompt - is there a profile set up?)

  2. new-item -path $profile -itemtype file -force (assuming the answer to the above is false)

  3. notepad $profile (opens Notepad)

  4. Paste in

     function prompt {
       $p = Split-Path -leaf -path (Get-Location)
       "$p> "
     }
    
  5. Save (you shouldn't have to chose a location; it is already done for you)

  6. Reload Visual Studio Code - you will probably get a message about running scripts (or just do next step before reload)

  7. Set-ExecutionPolicy RemoteSigned -Scope CurrentUser (at your PowerShell prompt, from the Super User answer)

  8. 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.

1

The below single line works fine for me:

(Get-Location | Get-Item).Name
2
  • that wasn't the question - see above correct answer from Jelgab
    – hfrmobile
    Oct 8 '18 at 12:01
  • This is a good way for one instance via powershell prompt
    – Zimba
    Mar 17 '20 at 14:03
1

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.