Question

I'm trying to open PowerShell with a customised prompt (for instance the UNIX shell prompt). I have tried:

powershell -noexit -command "& {function prompt {"$(pwd)$ "}}"

But it just starts powershell without the prompt I want. It does actually work in powershell itself. Could I get this to work or do I have to make a seperate file and do it through "-file"?

Was it helpful?

Solution

Not sure what the UNIX prompt defaults too but this should do what I think you want it to do.

powershell -noexit -command "function prompt {'{0}$ ' -f $pwd}"

If you use single quotes in the prompt function the $ doesn't get interpolated, and you don't have to worry about to many quotes.

SAVING THE PROMPT FUNCTION

Like any function, the Prompt function exists only in the current session. To save the Prompt function for future sessions, add it to your Windows PowerShell profiles. For more information about profiles, see about_Profiles.

Here's how to create a new profile:

if (!(test-path $profile)) 
       {new-item -type file -path $profile -force}
notepad $profile

OTHER TIPS

Quoting on the command-line is tricky. Also, & runs a scriptblock in its own scope, so functions defined there don't "leak" out to the calling scope. The dot operator (also called dot-sourcing) is what you're looking for. This is what I got to work using backslashes to quote the strings.

powershell -noexit -command ". {function prompt {\"$(pwd)$ \"}}"

Add your custom prompt to your profile and it will load/run every time you start PowerShell.

Powershell customisation is always a bit tricky. Try adding a script with a method called prompt() like this:

function prompt() {
$myPrompt = "Ready>";
write-host -NoNewLine -ForegroundColor green $myPrompt
' '
}

Then call this in a profile, such as the one for all users:

%windir%\system32\Windows­PowerShell\v1.0\profile.ps1

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top