Change colors in PowerShell

Done recently discovered this shell. He immediately got to look for books, which, by the way, have already been translated and can be taken from here . And everything seems to be fine, and the code is easy to write and integration with vsb-scripts, but, for some reason, it draws me to mastday windows.

And so, it was decided to repaint the PowerShell window to the standard cmd color and set up autostart of its own functions and aliases in parallel. It was also slightly annoying input suggestions with the inscription - PS, which, together with the color, resembles the Adobe Photoshop logo (I have nothing against Adobe).

So, the information following is exclusively for beginners.

First of all, we launch the shell, we see the familiar window:
image

First , we need the rights to run individual scripts with the ps1 extension.

You can view them by running the command:
Get-ExecutionPolicy

The default policy is: Restricted , which prohibits the execution of any scripts.

There are three options.
1. Either change the policy to AllSigned and sign scripts (you can read more by running get-help about_signing command ).
2. Either name the policy on RemoteSigned , which allows you to run all scripts, except for downloaded from the network.
3. Or change the policy to Unrestricted , which allows you to execute any scripts, except for downloaded ones, for the execution of which a request will be issued.

I chose the second path, it is, nevertheless, safer. Therefore, we change the policy with the command:

Set-ExecutionPolicy RemoteSigned

image
We receive a request to change the policy to which you want to answer Y (yes).

Secondly , create a script that will run along with PowerShell:
New-Item -type file $PROFILE

In theory, at this moment, such an error may pop up:
image
But it’s okay, the shell writes that the WindowsPowerShell folder does not exist in My Documents , create it and repeat the command:
image
As we see, an empty file called Microsoft.PowerShell_profile.ps1 was created .

We did half the work. We have the rights to execute scripts and a script has been created that is loaded by default.

Thirdly , we need to learn how to receive and change information about the shell.
The Get-Host cmdlet does just that:
image
The UI property is actually an object, and if we enter (Get-Host) .UI, then we will see that this object also has the RawUI property , which just contains the settings we need:
(Get-Host).UI.RawUI

image
You can change these settings as follows:

(Get-Host).UI.RawUI.WindowTitle = “Окно”
(Get-Host).UI.RawUI.BackgroundColor = “Black”
cls

It is important that in order for the changed settings to be immediately displayed on the screen, we need to register the cls alias responsible for clearing the screen.

This, of course, is all cool, but the settings are reset if you close the window, so write them to a file that loads with the shell at startup. We edit the Microsoft.PowerShell_profile.ps1 file like this:

# We to Hell?! - Is't WorldCount!
# Изменяем настройки окна
(Get-Host).UI.RawUI.ForegroundColor="Gray";
(Get-Host).UI.RawUI.backgroundColor="Black";
(Get-Host).UI.RawUI.CursorSize=10;
(Get-Host).UI.RawUI.WindowTitle="WorldCount Console";
# Очищаем экран
cls
# Выводим приветствие
echo " ";
echo "Привет, WorldCount!";
echo " ";
echo " ";
# Устанавливаем начальный каталог
$MyRoot = "C:\Shell\";
CD $MyRoot;
# Вид предложения ввода
function prompt
{
    "[WorldCount:] " + $(get-location) + "> "
}

And now briefly on the code.

The first 4 properties are responsible for setting up the window, after setting the properties we do the mandatory clearing - cls .

Cd $ MyRoot; - is responsible for what directory we will find ourselves in when loading the shell.

The promt function is a standard mutated function that is responsible for the appearance of the input line. I removed the PS and added my nickname.

Save the file, restart the shell.

It looks like this:
image
And lastly, in the Microsoft.PowerShell_profile.ps1 file , you can easily write your aliases. Let's say

set-alias WeToFuck Get-Process


Thank you all, good luck in mastering this beautiful shell.

Also popular now: