All Articles

How to freely customize the PowerShell prompt display

This page has been machine-translated from the original page.

Customize the PowerShell prompt display

Let’s jump right in and start learning PowerShell.

First, let’s simplify the prompt display.
By default, it is probably shown as PS [current directory], like this.

PS C:\Users\yuki> 

Here, you can remove the current-directory display by entering the following command.

PS C:\Users\yuki> function Prompt { "PS > " }
PS >

To restore the original state, enter the following.

PS > function prompt { "PS " + $(Get-Location) + "> " }

This returns the prompt to its original form.

Next, let’s customize the display using another command.

If you enter the following command, the output of $(echo "test") will be shown in the prompt.

PS C:\Users\yuki> function prompt { "PS " + $(echo "test") + "> " }
PS test>

Using this approach, you can freely change how the PowerShell prompt is displayed.

By the way, Get-Location, which we used to display the current directory, has two other aliases for the same command.

They are gl and pwd.

So even if you enter the following, the current directory will still be displayed in the prompt just as when using Get-Location.

PS C:\Users\yuki> function prompt {
>> "PS " + $(pwd) + "> " }

PS C:\Users\yuki> function prompt {
>> "PS " + $(gl) + "> " }

PS C:\Users\yuki>

When it comes to these aliases, a natural question is whether you really need to know all of them.
If you are the only one using them, honestly, knowing just one is probably enough.

However, if you are going to use PowerShell in the security field, it seems you need to know all of the aliases.

The idea is that even if you do not use a particular alias yourself, an attacker might use it, so understanding them is important if you want to recognize an attacker’s intent.

For more details, I referred to the following article.

Learning the basics of PowerShell at the Yamato Security Study Group · Akaki I/O

Summary

This time, I covered how to change the prompt display and touched on aliases.

PowerShell has become open source, and because it is a powerful shell and scripting environment that works not only on Windows but also on Linux and macOS, I want to keep learning more and more about it in the future.