PowerShell – Configure to Display Only Current Folder Name

powershellwindows 7

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

Best Answer

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

Related Question