Skip to content

Instantly share code, notes, and snippets.

@euaaron
Last active November 4, 2022 04:44
Show Gist options
  • Select an option

  • Save euaaron/d6a295828289b1309757b1d0a50a9666 to your computer and use it in GitHub Desktop.

Select an option

Save euaaron/d6a295828289b1309757b1d0a50a9666 to your computer and use it in GitHub Desktop.
Posh Beautify - A PowerShell script to make your shell awesome!
# Author: Aaron Carneiro <hello@aaroncarneiro.com>
# Name: Posh Beautify - A PowerShell script to make your shell awesome!
# Description: This script installs and configure Oh-My-Posh with Starship, and JetBrainsMono Nerd Font along with it's dependencies.
# Version: 1.0.0 - APR 04 2022
#Requires -RunAsAdministrator
# Function to check if a dependency is installed and install it if it's not
Function Resolve-Dependency
{
Param(
[Parameter(Mandatory=$true)]
[string]$Name,
[Parameter(Mandatory=$true)]
[string]$Run,
[Parameter(Mandatory=$true)]
[string]$Install,
[Parameter(Mandatory=$true)]
[string]$Upgrade,
[Parameter(Mandatory=$false)]
[string]$RequiredTo = ""
)
if((Test-CommandExists -Command $Run).Equals($true)) {
Write-Host "$Name is already installed. " -ForegroundColor Cyan
$willUpgrade = Read-Host "Do you want to upgrade it? (Recommended) [Y/n]"
$willUpgrade = $willUpgrade.ToLower()
if($willUpgrade.Equals("y")) {
Invoke-Expression $Upgrade
}
} else {
Write-Host "$Name is not installed. " -ForegroundColor Red -NoNewline
if($RequiredTo.Length -gt 0) {
Write-Host "It is required to $RequiredTo." -ForegroundColor Red
}
$Install = Read-Host "Do you want to install it? [Y/n]"
$Install = $Install.ToLower()
if ($Install.Equals("y")) {
Write-Host "Installing $Name..."
Invoke-Expression $Install
} else {
Write-Host "Ok, skipping $Name..."
}
}
} #End Function Resolve-Dependency
Function Test-CommandExists
{
Param ($Command)
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
try {if(Get-Command $Command){$true}}
Catch {$false}
Finally {$ErrorActionPreference=$oldPreference}
} #End Function Test-CommandExists
# Main #################################################################################################################
Write-Host "Hello " -NoNewline
Write-Host "$env:USERNAME" -ForegroundColor Blue -NoNewline
$Accept = Read-Host ", ready to make this terminal more interesting? [Y/n]"
$Accept = $Accept.ToLower()
if ($Accept.Equals("y")) {
if (!$env:OS.ToLower().Contains("windows")) {
Write-Warning "Sorry, this script is only for Windows."
exit 0;
}
# Install or Update Dependencies
Write-Host "Verifying dependencies..."
Resolve-Dependency -Name "Oh-My-Posh" -Run "oh-my-posh" -Install "winget install JanDeDobbeleer.OhMyPosh -s winget" -Upgrade "winget upgrade JanDeDobbeleer.OhMyPosh -s winget" -RequiredTo "install Spaceship Prompt"
Resolve-Dependency -Name "Chocolatey" -Run "choco" -Install "Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))" -Upgrade "choco upgrade chocolatey" -RequiredTo "install JetBrainsMono Nerd Font"
# Refreshing system environment variables
$Env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
# Install Starship
Write-Host "Adding Oh-My-Posh with Spaceship to your PowerShell Profile..." -ForegroundColor Cyan
if (!(Test-Path -Path $PROFILE)) {
New-Item -ItemType File -Path $PROFILE -Force
}
$CurrentProfile = Get-Content -Path $PROFILE -Raw
Set-Content -Path $PROFILE -Value "oh-my-posh init pwsh --config '$env:POSH_THEMES_PATH\spaceship.omp.json' | Invoke-Expression" -Force
Add-Content -Path $PROFILE -Value $CurrentProfile
Add-Content -Path $PROFILE -Value "clear" -NoNewline
# Install JetBrains Mono Nerd Font
$NerdFont = Read-Host "Do you want to install JetBrains Mono Nerd Font? [Y/n]"
$NerdFont = $NerdFont.ToLower()
if ($NerdFont.Equals("y")) {
Write-Host "Installing JetBrains Mono Nerd Font..."
choco install JetBrainsMonoNF -y
Write-Host "Remember to configure your terminal to use this font." -ForegroundColor Yellow
Write-Host "For help, go to https://learn.microsoft.com/en-us/windows/terminal/customize-settings/profile-appearance" -ForegroundColor Yellow
} else {
Write-Host "Ok, skipping JetBrains Mono Nerd Font..."
}
# Refreshing PowerShell Profile
Write-Host "Refreshing PowerShell Profile..." -ForegroundColor Cyan
try {
$PreviuosErrorPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
Invoke-Expression $PROFILE -ErrorAction SilentlyContinue
$ErrorActionPreference = $PreviuosErrorPreference
} catch {
Write-Warning "Warning! Something went wrong. Please, restart your shell."
}
Write-Host "Done! Enjoy your modern shell!" -ForegroundColor Green
Write-Host "To make it even nicer try adding a color scheme like https://draculatheme.com/windows-terminal"
Write-Host "Or try adding some PowerShell modules to improve your productivity! https://powershellgallery.com"
} else {
Write-Host "Ok, maybe next time."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment