Search code examples
powershellprompt

Is it possible to overwrite the Prompt function from a function in PowerShell?


I'm trying to define a single command to set a custom prompt on the PowerShell console. You can overwrite the default prompt with the following script:

function Prompt {
    "PS " + "[$(Get-Date)] " + $(Get-Location) + ">"
}

So I would like to call above script with a function, e.g.: Set-CustomPrompt.

I've already seen a solution on StackOverflow where the script was executed from an external ps1 file, but I am looking for a solution where the definition is in the same file.


Solution

  • Have your Set-CustomPrompt function define the Prompt function in the global scope (see also):

    function Set-CustomPrompt {
        function global:Prompt {
            "PS [$(Get-Date)] $(Get-Location)>"
        }
    }