Search This Blog

Monday, June 6, 2022

PowerShell: Custom profile

A custom profile allows you to set certain attributes, load some of your own self-made functions in advance and make your prompt a bit cooler.


Basically you place a .ps1 file named "Microsoft.PowerShell_profile.ps1" in your PowerShell folder.

This applies to Windows PowerShell ISE:

\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

It applies to regular Windows PowerShell:

\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

It applies to PowerShell 7 (cross-platform):

\PowerShell\Microsoft.PowerShell_profile.ps1 

Being in admin mode doesn't seem to change the profile path. To find the profile path on your computer enter $profile and hit enter.

The code is basically a bunch of settings put together. You can customize it to your needs. In order to change the prompt, which is the bit of text to the left of what you enter, customize a function called prompt. First part sets your window title to the current version of PowerShell installed. It beeps a few times at startup. A custom theme of colors is applied. A few functions are placed in the $profile script to illustrate that you can preload some of them in advance. If you want to get rid of the text popping up at the top of PowerShell at startup, use the command line switch -nologo in your shortcut.


$ErrorView = "ConciseView"
$ErrorActionPreference = "Stop"

$Major = $psversiontable | select-object -ExpandProperty psversion | Select-Object -ExpandProperty major
$Minor = $psversiontable | select-object -ExpandProperty psversion | Select-Object -ExpandProperty minor
$Patch = $psversiontable | select-object -ExpandProperty psversion | Select-Object -ExpandProperty patch

$host.UI.RawUI.WindowTitle = (
    "PowerShell $Major.$Minor.$Patch"
)


# Beeps three times at startup
1..3 | foreach-object {[console]::beep(400,100)};

$host.ui.RawUI.BackgroundColor = "Black"
$host.ui.RawUI.ForegroundColor = "Yellow"
$host.ui.RawUI.CursorSize = 10 # 1-100

<#
$host.PrivateData.ErrorForegroundColor = "Green"
$host.PrivateData.ErrorBackgroundColor = "Red"
$host.PrivateData.ErrorAccentColor = "Blue"
$host.PrivateData.WarningBackgroundColor = "Yellow"
$host.PrivateData.WarningForegroundColor = "Red"
$host.PrivateData.FormatAccentColor = "Cyan"
#>


Set-PSReadLineOption -BellStyle None
# The following are hex colors
Set-PSReadLineOption -Colors @{
    "Command" = "#D2691E" # Chocolate
    "Comment" = "#A0A0A0" # Gray
    "ContinuationPrompt" = "#FA8072" # Salmon, The >> that shows up at linebreak
    "Default" = "#6B8E23" # Olive, like pipeline etc
    "Emphasis" = "#FFCCE5"
    "Error" = "#FF9999"
    "Keyword" = "#FFA500" # Orange, keywords such as -or/-and
    "Member" = "#9999FF" # When you ().member
    "Number" = "#E0E0E0" # gray
    "Operator" = "#FFE4C4" # bisque
    "Parameter" = "#FFC0CB" # Pinkish
    "Selection" = "#CCFFCC"
    "String" = "#CCFF99"
    "Type" = "#99CCFF" # Lightblue when you [cast]
    "Variable" = "#7FFF00" # Clear greenish
}

Set-Location D:\Temp\
function prompt
{
    $colors = "DarkGreen", "DarkCyan", "DarkRed", "DarkMagenta", "DarkYellow", "Gray", "DarkGray", "Green", "Cyan", "Red", "Magenta", "Yellow", "White"; #no Black, DarkBlue or Blue
        [console]::CursorVisible = 0;
        $Location = Get-Location;
        $Date = Get-Date -Format "HH:mm:ss";
    (write-host "|" -ForegroundColor "magenta" -nonewline);
    (write-host ">" -ForegroundColor "white" -BackgroundColor "darkmagenta" -NoNewline);
    (write-host "_" -ForegroundColor "White" -BackgroundColor "darkcyan" -NoNewline);
    (write-host "|" -ForegroundColor "cyan" -nonewline);
    (write-host "$Date " -foregroundcolor ($colors | get-random) -nonewline);
    (write-host "| " -foregroundcolor ($colors | get-random) -nonewline);
    (write-host "$location" -foregroundcolor ($colors | get-random) -nonewline);
    (write-host " > " -foregroundcolor ($colors | get-random) -nonewline);
        " ";
      }   

function Start-WoW {start-process "D:\BattleNet\World of Warcraft\_retail_\wow.exe"}


function Start-Blinking {1..50 | foreach-object {$host.ui.RawUI.BackgroundColor = ($colors = "DarkGreen", "DarkCyan", "DarkRed", "DarkMagenta", "DarkYellow", "Gray", "DarkGray", "Green", "Cyan", "Red", "Magenta", "Yellow", "White", "Black", "DarkBlue", "Blue" | get-random); start-sleep -milliseconds 100; Clear-Host}}


function Start-Nightsky {1..100 | ForEach-Object {start-sleep -milliseconds 100; write-host ("x").PadLeft((1..200 | get-random)) -ForegroundColor ("yellow","white","red","cyan","blue" | get-random)}}

No comments:

Post a Comment