Customizing Windows PowerShell command prompt

Windows PowerShell is a great tool and, if you are using it frequently, one of the things you might want to do is to customize the default command prompt. This is actually a quite common activity among Linux users; hence, there are many tutorials on doing that as well. In case you want to do this kind of customization to Windows PowerShell, please follow the guidance below.

The text displayed as the prompt for each line in the PowerShell is defined with Prompt() function. You are free to override this function (hence, change the command prompt) the same way you can override any other function – by defining the function in the default user profile script. The profile scripts can be defined at one of the following locations:ir-leasing.ru

You can take any (depending on the scope you want to affect) of these files (I prefer using %windir%\system32\WindowsPowerShell\v1.0\profile.ps1) and at the following text to it, you would change your PowerShell command prompt to # (hash symbol):ЛИАНЫ

The next time your launch PowerShell, it would look like this:
Powershell hash promptIt is very likely, though, that you would want to make your command prompt somehow more useful instead of just showing a hash symbol. I tried few different configurations, but for now I have stick with the following function for now:

This will result into the following prompt, which includes the time, working directory as well as the number of objects in the working directory:
Powershell custom promptIt is up to you what information you want it to show or what colors to use. Feel free to use the comments section to share the scripts you use in your environment.

UNIX-based shells often include the hostname and the username in the prompt as well, but I do not think there really is a point seeing this information on every line typed in PowerShell. I think a much better place for this information is the title bar of the shell and it can be achieved easily by adding the following lines to the same profile script you used for modifying the command prompt:

This will make your PowerShell titles look like this:
Powershell custom titleIn addition, the title will include PowerShell version as well as “(Administrator)” text if the PowerShell is being run elevated.

Once you come up with your perfect default PowerShell profile script, including all your customization, there are chances you might want to apply this to multiple computers/servers in your environment. There are multiple ways this can be done, but probably on of the easiest is by using Group Policy Preferences (assuming you have Active Directory deployed):

One thing you should keep in mind if you are doing this is that this will overwrite %windir%\System32\WindowsPowerShellv1.0\profile.ps1 if it already exist on a machine, losing all the modifications done there.

Social

8 comments on the post

  1. Thanks for this; it is a nice implementation.

    I made a slight adjustment on line 7 of the prompt script to show only the current folder name.

    Write-Host (split-path($PWD) -Leaf) -nonewline -foregroundcolor Gray

  2. Your system-wide (all user) profile paths have an error:

    %windir%\system32\WindowsPowerShellv1.0
    Should be:

    %windir%\system32\WindowsPowerShell\v1.0
    You might also want to note for your readers that the profile in this location could be overwritten by Microsoft at any time.

    You mentioned that you “… do not think there really is a point seeing this information [hostname and username] on every line typed in PowerShell.” The reason you might want to see this is when you have many systems to manage and you want to be certain that you are interacting with the one that you intend to. This comes in especially-handy on a 1 AM emergency call. :) You can achieve a similar in the Command Prompt result using the COMPUTERNAME environment variable, by placing one of the following in the CMD shortcut:

    :: Username@Hostname:
    %windir%\system32\cmd.exe /k prompt %USERNAME%@%computername%$g & cls

    :: Hostname Only
    %windir%\system32\cmd.exe /k prompt %computername%$g & cls

    Hope that helps.

  3. I can’t even find profile.ps1 file in v0.1 folder. There are some language folders and a folder named a “Example” which has that file, but i am not able to save the changes to it.

  4. For frequent PowerShell users, the standard settings might not be ideal. We can change the settings of our PowerShell window to how we like it by modifying the profile.

Leave a Reply

Your email address will not be published. Required fields are marked *