PowerShell – Prompts, Paths, Profiles

PowerShell – PowerShell_ISE, Profiles and Prompts

By default, your PowerShell prompt is “PS <<path>> >”. So normal; so dull. However, that can be easily changed. In my case, I want to add:

  • the current time (well, the time at which the prompt was generated)
  • a reminder which account I’m using (I have two – a “normal user” one, and a “DBA/SU” one
  • a countdown of seconds remaining until my next holiday

Oh, and I still need to see the current path.

The obvious place to do this is in the profile file that PowerShell_ISE loads when you launch it; this can contain various bits of functionality – for example, code to import modules you always need, or to connect up to Azure so you can do stuff up there as well as on-prem.

In this case, we’re using it to change the PS prompt.

First things first.

Create a Profile

I’m assuming you’ve not already got one…

Check what your profile path is by running this:


Who’s a naughty boy? Running in his su account? Tsk.

Now, if you try to open that file as a new PoSh ISE user, chances are, it won’t exist. Which is to be expected. Slightly less expected is that the directory path also doesn’t exist.

Fortunately, there’s a quick one-liner to remedy that problem:

New-Item ($profile) -ItemType File -Force

The -Force parameter creates the required directory path; passing in $Profile like this saves the possibility of a typo somewhere along the way. And it’s quicker / easier to type.

Now, use this little PoSh_ISE-specific command to open the file you’ve just created in the editor:

psEdit ($profile)

Lo and behold, an empty file. Ready for you to fill with your lovely code.

Changing the Prompt

In PoSh-land, the prompt is generated by a function called, unbelievably, “prompt”. So all we need to do is create our own version of that function.

In your shiny new ISE profile file, type the following:

function prompt {
    "PS LoneDBA>"

Now save your profile file.

That won’t automatically change your prompt. You have to run the script to load the new function definition. For shame. Press F5.


Right. Not what we want as our end result, but at least we’ve changed the prompt. Progress.

So, time to start adding things into the prompt. First, we’ll add a space to the end, so that there’s a clearer separator between the > and the beginning of your PoSh input; like you normally see… No, you don’t need to see that separately.

Next up, we’ll add the path, traditionally shown at the end of the prompt:

function prompt {
    "PS LoneDBA $(Convert-Path .)> "

Save, and re-run.


Things start getting a bit long-winded, so we’re going to split the prompt onto multiple lines, so it’s a bit easier to follow along. One line per feature.

function prompt {
    "PS LoneDBA " +
        "$(Convert-Path .)" +
        "> "

Let’s add in the user name:

function prompt {
    "PS LoneDBA " +
        "$($env:username) " +
        "$(Convert-Path .)" +
        "> "

and the current time (I can usually remember the date…)

function prompt {
    "PS LoneDBA " +
        "$((get-date).tostring().substring(11)) " +
        "$($env:username) " +
        "$(Convert-Path .)" +
        "> "

This one was a bit more complicated, but what you need to know is that get-date returns a Datetime-type object. So we need to convert that to a string, and strip off the first 11 characters, just leaving the time.


Getting there.

Here’s the fun part – seconds until the next holiday:

function prompt {
    "PS LoneDBA " +
        "$((get-date).tostring().substring(11)) " +
        "$($env:username) " +
        "$([Convert]::ToInt32(([datetime]'2016-10-28 16:30' - (get-date)).duration().TotalSeconds)) " +
        "$(Convert-Path .)" +
        "> "

It’s a bit of a hack, but conceptually simple – a subtraction of the current date from the holiday start date, and then convert the difference into a number of seconds; we’ll convert that into an integer, as we don’t want to see fractional seconds…)



4.9 million seconds might seem like a lot, but it goes down quickly – 50k+ overnight, 200k+ over a weekend…

Multiple Profiles

You have multiple profiles, depending on the PoSh environment you’re running. I’m not going to think about that here – but there’s more reading in this MSDN article – How to Use Profiles in Windows PowerShell ISE

This entry was posted in SQLServerPedia Syndication and tagged . Bookmark the permalink.

4 Responses to PowerShell – Prompts, Paths, Profiles

  1. Pingback: Powershell Prompts – Curated SQL

  2. Michael Sorens says:

    From one Friend of Redgate to another:
    You’ve got some nice tips on setting the prompt. However, I did want to point out that the article you refer readers to for more details on using the PowerShell profile is, while useful, rather short. For those that want an in-depth understanding of the potential complexities of the profile, take a look at (shameless plug) my recent article on Simple-Talk.com, “Persistent PowerShell: The PowerShell Profile” at https://www.simple-talk.com/sysadmin/powershell/persistent-powershell-the-powershell-profile/

  3. Pingback: PowerShell – Slicing | The Lone DBA

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.