Configure Windows PowerShell to display only the current folder name in the shell prompt

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


Solution 1:

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

Solution 2:

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 )>" }

Solution 3:

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.