0

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"?

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

0

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!

1 Comment

I would not recommend putting it in the system profile, do it in your user profile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.