DEV Community

krlz
krlz

Posted on

Automate Your Terminal Setup with PowerShell: Installing Starship Prompt

Introduction

PowerShell scripts (.ps1 files) are powerful automation tools for Windows users. In this article, I will share a practical script that installs and configures Starship, a minimal, blazing-fast, and customizable prompt for any shell.

What is a .ps1 File?

A .ps1 file is a PowerShell script - a text file containing a series of PowerShell commands that execute sequentially. Think of it as a batch file on steroids:

  • Extension: .ps1 stands for PowerShell version 1 (though it works with all versions)
  • Execution: Run with .script.ps1 in PowerShell
  • Security: Windows blocks scripts by default - you need to set an execution policy

What is Starship?

Starship is a cross-shell prompt written in Rust that:

  • Works with PowerShell, Bash, Zsh, Fish, and more
  • Shows git status, language versions, and system info
  • Is incredibly fast and highly customizable
  • Uses Nerd Fonts for beautiful icons

Starship Prompt

The Installation Script

Here is a complete PowerShell script to install and configure Starship:

# Install Starship Prompt
# Run this script in PowerShell: .install-starship.ps1

Write-Host "Installing Starship..." -ForegroundColor Cyan

# Install Starship via winget
winget install --id Starship.Starship --accept-source-agreements --accept-package-agreements

# Set execution policy to allow scripts
Write-Host "Setting execution policy..." -ForegroundColor Cyan
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force

# Create PowerShell profile if it does not exist
if (!(Test-Path -Path $PROFILE)) {
    New-Item -Path $PROFILE -ItemType File -Force | Out-Null
    Write-Host "Created PowerShell profile at $PROFILE" -ForegroundColor Green
}

# Add Starship init to profile if not already present
$profileContent = Get-Content -Path $PROFILE -Raw -ErrorAction SilentlyContinue
if ($profileContent -notmatch "starship init powershell") {
    Add-Content -Path $PROFILE -Value "`nInvoke-Expression (&starship init powershell)"
    Write-Host "Added Starship to PowerShell profile" -ForegroundColor Green
} else {
    Write-Host "Starship already configured in profile" -ForegroundColor Yellow
}

Write-Host "`nInstallation complete!" -ForegroundColor Green
Write-Host "Restart PowerShell to see Starship in action." -ForegroundColor Cyan
Enter fullscreen mode Exit fullscreen mode

Script Breakdown

Let me explain each part:

1. Installing with Winget

winget install --id Starship.Starship --accept-source-agreements --accept-package-agreements
Enter fullscreen mode Exit fullscreen mode

winget is Windows Package Manager - the official way to install software on Windows 10/11. The flags auto-accept prompts for automation.

2. Execution Policy

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
Enter fullscreen mode Exit fullscreen mode

Windows blocks PowerShell scripts by default. RemoteSigned allows local scripts while requiring signatures for downloaded scripts. CurrentUser scope means no admin rights needed.

3. PowerShell Profile

if (!(Test-Path -Path $PROFILE)) {
    New-Item -Path $PROFILE -ItemType File -Force | Out-Null
}
Enter fullscreen mode Exit fullscreen mode

$PROFILE is an automatic variable pointing to your PowerShell profile script (runs on every PowerShell start). We create it if missing.

4. Idempotent Configuration

if ($profileContent -notmatch "starship init powershell") {
    Add-Content -Path $PROFILE -Value "`nInvoke-Expression (&starship init powershell)"
}
Enter fullscreen mode Exit fullscreen mode

We check if Starship is already configured before adding. This makes the script idempotent - safe to run multiple times.

How to Use

  1. Save the script as install-starship.ps1
  2. Open PowerShell
  3. Navigate to the script location
  4. Run: .install-starship.ps1
  5. Restart PowerShell

Bonus: Install a Nerd Font

For the best experience, install a Nerd Font:

winget install JetBrains.JetBrainsMono.NerdFont
Enter fullscreen mode Exit fullscreen mode

Then set it as your terminal font in Windows Terminal settings.

Conclusion

PowerShell scripts are excellent for automating repetitive tasks. This Starship installation script demonstrates key concepts:

  • Using winget for software installation
  • Managing execution policies
  • Working with PowerShell profiles
  • Writing idempotent scripts

Feel free to use and modify this script for your own terminal setup!


Happy coding!

Top comments (0)