I'd like to have the current working directory show on the powershell command line, as would be done in a (for example) CMD prompt. How can I do so?
6 Answers
Check this out: http://mshforfun.blogspot.com/2006/05/perfect-prompt-for-windows-powershell.html
Basically, you can create a 'profile' file called Microsoft.PowerShell_profile.ps1 which will run every time you start powershell.
Depending on whom you want it to run for, there are several folders you can put this file in (explained in the link above). If it's just for yourself, you can create a folder called WindowsPowerShell in your My Documents folder, and put it there.
If you put this function in that file:
function prompt
{
"PS " + $(get-location) + "> "
}
It will make your prompt look like this:
PS C:\directory\path\here>
There's a whole lot of other stuff you can put in it, but that's the basics.
NOTE: before you can use the profile script, you'll need to run "set-executionpolicy remotesigned" from the powershell - this will allow you to run unsigned scripts written locally on the computer and signed scripts from others.
-
Doesn't seem to work for Visual Studio's add-in :( Oh well. Commented Jan 26, 2011 at 4:08
-
@BillyONeal: Check the value of
$profile.CurrentUserCurrentHost
and$profile.CurrentUserCurrentHost
to see what (user) profile scripts are valid. Different hosts (e.g. VS vs. ISE) have a different value for$profile.CurrentUserCurrentHost
– RichardCommented Jan 26, 2011 at 10:51
Simple, add the following to your profile.ps1
file (under your My Documents\WindowsPowerShell folder):
function prompt { "$pwd>" }
Try the following:
$CurrentDir = $(get-location).Path;
Nowadays, this works fine:
echo "$PWD"
which works differently than
echo $PWD
Just don't forget the quotes :) Sample output below.
PS C:\Users\user name> echo $PWD
Path
----
C:\Users\user name
PS C:\Users\user name> echo "$PWD"
C:\Users\user name
PS C:\Users\user name>
To show only the current directory in PowerShell (or pwsh), put this in your profile.ps1
:
function prompt { 'PS - ' + (get-item .).name + ' >' }