17

So I've just downloaded Visual Studio Code to use as my default IDE for learning Python. I'm running on a 64-bit machine so I made the default terminal windows powershell.

The place where I'll be saving most of my files is about 8 folders deep which all show up in the terminal before any commands can be written. Is there any way to hide or shorten the file path in the terminal?

Mark
  • 143,421
  • 24
  • 428
  • 436
tarruda23
  • 313
  • 1
  • 3
  • 10

2 Answers2

45

As @Biclops suggested, there is good info here: configure PowerShell to only show the current folder in the prompt

However, I needed more basic info to get this to work. This is a very good resource to get started: Windows PowerShell Profiles. So I first followed the steps suggested there:

[always using vscode's integrated terminal using PowerShell]

  1. test-path $profile (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 (from the SuperUser answer above)

    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 vscode - you will probably get an error message about running scripts (or just do next step before reload)
  7. Set-ExecutionPolicy RemoteSigned -Scope CurrentUser (at your integrated terminal PS prompt, also from the SuperUser answer)
  8. reload vscode
  9. You should be good to go!
Mark
  • 143,421
  • 24
  • 428
  • 436
2

Great Question and Great Answers.

But this information is dated, and Windows is currently updating from Windows 10 to Windows 11. In addition, the base Windows PowerShell has been incorporated into the new Windows Terminal Preview app. which is being used here.

The solution provided above by Mark and Tarruda23 (above) almost works. But Windows throws an error - described below.

The steps:

First, it was necessary to determine whether a profile existed. Using the Windows Explorer, the following path was checked. If a profile already exists, this path shows where an existing profile should be found. On this PC, no profile ( .ps1 ) file existed and this folder was empty. Don't close the Explorer.

C:\Users\prior\OneDrive\Documents\WindowsPowerShell

Since no file exists, a new file needed to be created. This new file must be saved with a specific name - shown below.

Navigate to the empty folder and open PowerShell. The .ps1 profile must be created and saved in this folder. Use the Powershell's build-in text editor to create the new file. Type:

ISE

Then type or paste the following into the empty text file:

function prompt {
  $p = Split-Path -leaf -path (Get-Location)
  "$p> "
}

Save this file with the following name:

Microsoft.PowerShell_profile.ps1

Use the PowerShell to open Notepad and check that .ps1 file. This demonstrates the Windows system has found the new .ps1. Next close the Notepad.

Notepad $profile

Now the PowerShell is probably displaying an error message in red text. This error message reads in part:

\Microsoft.PowerShell _profile.ps1 cannot be loaded because running scripts is disabled on this system.

Run the PowerShell as the Administrator. Type the following.

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Windows will prompt with a question:

Do you want to change the execution policy?

Type y for yes. This will change and remove the Windows default settings that prevents running script files. Once done, this will remove that error message.

All should be good now. PowerShell will now start and run with shorter and abbreviated PS> prompt that shows either the User name, or the name of the folder where the PowerShell is running.

Gray
  • 1,164
  • 1
  • 9
  • 23
  • 1
    Which part(s) of the other answer are outdated? Everything you're doing here are the same steps as the other answer except 1) you use Explorer instead of `Test-Path $profile`, 2) you use `ISE` instead of Notepad to create the profile script, and 3) your profile path has a `OneDrive` parent directory for `Documents`. This answer seems to be only superficially different and should, at best, be a comment on the other answer. – Lance U. Matthews Dec 02 '21 at 04:45
  • I've never had restrictions running script files on this PC before. That is until I tried to run the script designed to abbreviate the PS prompt. The answer provided above reflects the restrictions that were experienced and caused by Windows (perhaps something arising from the Windows10-11 upgrade, or perhaps from using Windows Terminal Preview) when running this script file. – Gray Dec 02 '21 at 05:08
  • 1
    But what were those restrictions on your PC? Which part of this answer addressed those restrictions that wasn't covered in the other answer? I don't see any appreciable difference between the two. – Lance U. Matthews Dec 02 '21 at 05:19