Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redefine prompt function during Powershell session

Tags:

powershell

Is it possible to redefine the prompt function specified in the user's profile after the shell has started?

like image 735
guillermooo Avatar asked Oct 10 '09 23:10

guillermooo


People also ask

How do I change the prompt in PowerShell?

To customize the prompt, write a new Prompt function. The function is not protected, so you can overwrite it. Then, between the braces, enter the commands or the string that creates your prompt. The following Prompt function displays the history ID of the next command.

How do I default to command prompt instead of PowerShell?

Press Ctrl + Shift + 2 while the Terminal window is in focus. This opens a new tab with the Command Prompt shell. You can now close the first tab and start working using CMD. To close the first tab, click on the X button next to the Windows PowerShell tab name.

How do I interrupt a PowerShell command?

You can interrupt and stop a PowerShell command while it is running by pressing Control-C. A script can be stopped with the command exit. This will also close the PowerShell console.

How do you call a parameterized function in PowerShell?

PowerShell uses the parameter value order to associate each parameter value with a parameter in the function. When you use positional parameters, type one or more values after the function name. Positional parameter values are assigned to the $args array variable.


2 Answers

To change the prompt function at any time, just execute a statement like the following at the PowerShell command line:

function prompt { "$env:computername $pwd>" }
like image 159
Jason Kresowaty Avatar answered Oct 13 '22 08:10

Jason Kresowaty


Yes, just enter code as normal:

function prompt
{
    $random = new-object random
    $color=[System.ConsoleColor]$random.next(1,16)
    Write-Host ("PS " + $(get-location) +">") -nonewline -foregroundcolor $color
    return " "
}

(credit to http://mshforfun.blogspot.com/2006/05/perfect-prompt-for-windows-powershell.html)

like image 43
sahmeepee Avatar answered Oct 13 '22 08:10

sahmeepee