Skip to content

Instantly share code, notes, and snippets.

@ddieppa
Last active March 26, 2025 12:28
Show Gist options
  • Save ddieppa/c07a8caba016f6b723df3f523b5ce2e0 to your computer and use it in GitHub Desktop.
Save ddieppa/c07a8caba016f6b723df3f523b5ce2e0 to your computer and use it in GitHub Desktop.
OhMyPosh Configuration and PowerShell Profile
# Handle PowerShell 7.4+ UTF-8 encoding issues
$previousOutputEncoding = [Console]::OutputEncoding
[Console]::OutputEncoding = [Text.Encoding]::UTF8
try {
# # PowerToys Command Not Found Integration (with safe handling)
# $powerToysModulePath = Join-Path $env:LocalAppData "PowerToys\WinGetCommandNotFound.psd1"
# if (Test-Path $powerToysModulePath) {
# try {
# # Enable PowerShell Experimental Features for PowerToys
# Enable-ExperimentalFeature PSCommandNotFoundSuggestion -ErrorAction SilentlyContinue
# Enable-ExperimentalFeature PSFeedbackProvider -ErrorAction SilentlyContinue
# Import-Module $powerToysModulePath -ErrorAction Stop
# Write-Host "✅ PowerToys Command Not Found module loaded successfully" -ForegroundColor Green
# } catch {
# Write-Warning "⚠️ Failed to load PowerToys module: $_"
# }
# }
# Initialize Oh My Posh with theme
try {
# oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/jandedobbeleer.omp.json" | Invoke-Expression
# oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/ys.omp.json" | Invoke-Expression
# oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/stelbent-compact.minimal.omp.json" | Invoke-Expression
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\microverse-power.omp.json" | Invoke-Expression
Write-Host "✅ Oh My Posh initialized successfully" -ForegroundColor Green
} catch {
Write-Warning "⚠️ Failed to initialize Oh My Posh: $_"
}
# Enable Terminal Icons module
try {
Import-Module -Name Terminal-Icons -ErrorAction Stop
Write-Host "✅ Terminal Icons module loaded successfully" -ForegroundColor Green
} catch {
Write-Warning "⚠️ Failed to load Terminal Icons module: $_"
}
# PSReadLine configuration
try {
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -PredictionViewStyle ListView
Set-PSReadLineOption -EditMode Windows
Write-Host "✅ PSReadLine configured successfully" -ForegroundColor Green
} catch {
Write-Warning "⚠️ Failed to configure PSReadLine: $_"
}
} catch {
Write-Warning "⚠️ An error occurred while loading profile: $_"
} finally {
# Restore original encoding
[Console]::OutputEncoding = $previousOutputEncoding
}

Modern Windows Terminal Setup Guide

Before You Start

Note: While these customizations work with PowerShell 5.1 (Windows default), upgrading to PowerShell 7+ is highly recommended for:

  • Better module compatibility
  • Improved performance
  • Modern features and syntax
  • Fewer compatibility issues
  • Better error handling

Checking Your PowerShell Version

$PSVersionTable.PSVersion

Optional: Upgrading to PowerShell 7+

If you want to upgrade (recommended):

winget install --id Microsoft.PowerShell --source winget

PowerShell Versions and Profiles

PowerShell Versions in Windows 11

Windows 11 can have multiple PowerShell versions installed simultaneously:

  1. Windows PowerShell 5.1 (Default Windows version)
    • Located at: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
    • Default profile paths:
# PowerShell 5.1 Profile Locations
AllUsersAllHosts       : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost    : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts    : C:\Users\$env:USERNAME\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\$env:USERNAME\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
  1. PowerShell 7+ (Modern version)
    • Located at: C:\Program Files\PowerShell\7\pwsh.exe
    • Default profile paths:
# PowerShell 7+ Profile Locations
AllUsersAllHosts       : C:\Program Files\PowerShell\7\profile.ps1
AllUsersCurrentHost    : C:\Program Files\PowerShell\7\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts    : C:\Users\$env:USERNAME\Documents\PowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\$env:USERNAME\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

OneDrive Integration

When using OneDrive with Documents folder sync:

  • The Documents paths will be redirected to C:\OneDrive\Documents\
  • Example for PowerShell 5.1:
CurrentUserAllHosts    : C:\OneDrive\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : C:\OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
  • Example for PowerShell 7:
CurrentUserAllHosts    : C:\OneDrive\Documents\PowerShell\profile.ps1
CurrentUserCurrentHost : C:\OneDrive\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

Checking Your Configuration

  1. Check PowerShell version:
$PSVersionTable.PSVersion
  1. View your profile locations:
$PROFILE | Select-Object *Host* | Format-List

Important Notes

  1. Each PowerShell version maintains its own separate profile files
  2. OneDrive sync can affect profile locations
  3. You may need to configure both profiles if using both PowerShell versions
  4. Some features (like PSReadLine prediction) are only available in PowerShell 7+
  5. Use $env:USERNAME to get your actual username in paths

Required Components

1. PSReadLine Module

Different PowerShell versions require different PSReadLine configurations:

For PowerShell 7+

# Check current version
Get-Module -Name PSReadLine -ListAvailable | Select-Object -Property Name,Version

# Update to latest version
Install-Module PSReadLine -Force -SkipPublisherCheck

# Configuration in profile
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -PredictionViewStyle ListView
Set-PSReadLineOption -EditMode Windows

For PowerShell 5.1

# Check current version
Get-Module -Name PSReadLine -ListAvailable | Select-Object -Property Name,Version

# Update to latest version that supports 5.1
Install-Module -Name PSReadLine -AllowPrerelease -Force -SkipPublisherCheck

# Configuration in profile (basic features only)
Set-PSReadLineOption -EditMode Windows
# Note: PredictionSource and PredictionViewStyle are not available in older versions

2. Oh My Posh

winget install JanDeDobbeleer.OhMyPosh -s winget

3. Nerd Fonts

Install via Oh My Posh:

oh-my-posh font install

Recommended fonts:

  • CascadiaCode
    • CaskaydiaCove NF
    • CaskaydiaCove NFP
    • CaskaydiaCove NFM
  • FiraCode
    • FiraCode Nerd Font Propo
    • FiraCode Nerd Font
    • FiraCode Nerd Font Mono
  • Meslo
    • MesloLGM Nerd Font
    • MesloLGS Nerd Font
    • Other Meslo variants

4. Terminal Icons

Install-Module -Name Terminal-Icons -Repository PSGallery

5. Additional PowerShell Modules

  1. Posh-Git for Git integration
  2. Z module for directory navigation:
Install-Module z -AllowClobber

Configuration

1. PowerShell Profile Setup

Create/edit your PowerShell profile:

notepad $PROFILE

Add these configurations:

# Oh My Posh initialization
oh-my-posh --init --shell pwsh --config C:\Users\YourUsername\AppData\Local\Programs\oh-my-posh\themes\jandedobbeleer.omp.json | Invoke-Expression

# Enable modules
$env:POSH_GIT_ENABLED = $true
Import-Module -Name Terminal-Icons
Import-Module z

# PSReadLine configuration
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -PredictionViewStyle ListView
Set-PSReadLineOption -EditMode Windows

2. Windows Terminal Font Configuration

Add this to your Windows Terminal settings:

"profiles": {
    "defaults": {
        "font": {
            "face": "MesloLGM Nerd Font"
        }
    }
}

Verification Steps

After setup, verify your installation:

  1. PowerShell version: $PSVersionTable.PSVersion
  2. Execution policy: Get-ExecutionPolicy
  3. PSReadLine: Get-Module PSReadLine

Troubleshooting

Common Issues with PowerShell 5.1

  1. Module Compatibility

    • Some newer modules might not work correctly
    • You may see warnings about compatibility
    • Some features might be limited or unavailable
  2. Performance Issues

    • Slower command prediction
    • Limited parallel processing capabilities
  3. PSReadLine Limitations

    • Some newer prediction features might not work
    • Limited syntax highlighting capabilities

General Troubleshooting

  1. If you encounter execution policy errors:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
  1. If modules fail to import:

    • Try running PowerShell as administrator when installing modules
    • Check module compatibility with your PowerShell version
    • Clear PowerShell module cache if needed
  2. Font issues:

    • Restart Windows Terminal after installing new fonts
    • Verify font installation in Windows Fonts settings
    • Check for correct font name in terminal settings

PowerToys WinGetCommandNotFound Module Error

Overview

PowerToys' Command Not Found feature helps suggest package installations when you type an unrecognized command in PowerShell. For example, if you type node but don't have Node.js installed, it suggests installing it via winget.

If you see this error:

Import-Module: The specified module 'C:\Users\[username]\AppData\Local\PowerToys\WinUI3Apps\..\WinGetCommandNotFound.psd1' was not loaded because no valid module file was found in any module directory.

Prerequisites

  1. PowerToys version 0.70.0 or later
  2. Windows 10 version 1903 (build 18362) or later
  3. PowerShell 5.1 or later
  4. Winget installed and configured

Causes

  1. PowerToys installation issues
  2. Module file has been moved or deleted
  3. Compatibility issues with PowerShell 7
  4. Experimental features not enabled
  5. Incorrect path references in PowerShell profile

Solutions

  1. Verify PowerToys Installation
# Check if the module exists
Test-Path "C:\Users\$env:USERNAME\AppData\Local\PowerToys\WinGetCommandNotFound.psd1"
  1. Enable Required Features in PowerToys
  • Open PowerToys Settings
  • Navigate to 'Command Not Found'
  • Enable the feature
  • Restart PowerShell
  1. Enable PowerShell Experimental Features
Enable-ExperimentalFeature PSCommandNotFoundSuggestion
Enable-ExperimentalFeature PSFeedbackProvider
# Restart PowerShell after enabling features
  1. Fix Profile Configuration Edit your PowerShell profile:
notepad $PROFILE

Add this safer conditional import:

# PowerToys Command Not Found Integration
$powerToysModulePath = Join-Path $env:LocalAppData "PowerToys\WinGetCommandNotFound.psd1"
if (Test-Path $powerToysModulePath) {
    Import-Module $powerToysModulePath
}
  1. Manual Module Installation If other solutions fail:
  2. Download PowerToys from GitHub Releases
  3. Run installer as administrator
  4. Enable Command Not Found in PowerToys settings
  5. Restart PowerShell

Profile Load Time Optimization

If experiencing slow profile loading:

# Measure specific module load time
Measure-Command { Import-Module ModuleName }

# Profile load time optimization tips
# 1. Use conditional imports
# 2. Remove unused modules
# 3. Consider async loading for non-critical modules

Testing the Feature

After setup, you can test the feature:

  1. Try running a non-existent command that has a known package
python
# Should suggest installing Python if not installed
  1. Verify module is loaded correctly:
Get-Module WinGetCommandNotFound

Additional Resources

Additional Resources

PowerShell Version-Specific Issues

  1. PSReadLine Parameter Errors in PowerShell 5.1 If you see errors like:
Set-PSReadLineOption : A parameter cannot be found that matches parameter name 'PredictionSource'.

This means you're using PowerShell 5.1 with modern PSReadLine features. Solutions:

  • Remove the unsupported parameters from your 5.1 profile
  • Upgrade to PowerShell 7+ for full feature support
  • Use conditional logic in your profile:
if ($PSVersionTable.PSVersion.Major -ge 7) {
    Set-PSReadLineOption -PredictionSource History
    Set-PSReadLineOption -PredictionViewStyle ListView
}
Set-PSReadLineOption -EditMode Windows

Profile Management with Git

Custom Profile Location

You can store your PowerShell profiles in a custom location and create symbolic links to the default locations. Here's how:

  1. Create a Git Repository
# Create a directory for your profiles
mkdir C:\Users\YourUsername\Documents\PowerShellProfiles
cd C:\Users\YourUsername\Documents\PowerShellProfiles

# Initialize git
git init
  1. Create Profile Files
# Create separate profiles for PS 5.1 and PS 7
New-Item -ItemType File -Path ".\Microsoft.PowerShell_profile.ps1"     # For PS 5.1
New-Item -ItemType File -Path ".\Microsoft.PowerShell_profile.pwsh.ps1" # For PS 7
  1. Create Symbolic Links
# For PowerShell 5.1
New-Item -ItemType SymbolicLink -Path "$env:OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1" -Target "C:\Users\YourUsername\Documents\PowerShellProfiles\Microsoft.PowerShell_profile.ps1"

# For PowerShell 7
New-Item -ItemType SymbolicLink -Path "$env:OneDrive\Documents\PowerShell\Microsoft.PowerShell_profile.ps1" -Target "C:\Users\YourUsername\Documents\PowerShellProfiles\Microsoft.PowerShell_profile.pwsh.ps1"

Git Integration

  1. Create .gitignore
# Create .gitignore
@"
# Local PowerShell files
*.log
*.tmp

# Module caches
**/Modules/*
"@ | Out-File .gitignore
  1. Initial Commit
git add .
git commit -m "Initial PowerShell profile setup"
  1. Add Remote Repository
git remote add origin https://github.com/YourUsername/powershell-profiles.git
git push -u origin main

Benefits

  1. Version control for your profiles
  2. Easy synchronization across machines
  3. Backup of your configurations
  4. Ability to share configurations with others
  5. Track changes and rollback if needed

Important Notes

  1. Make sure to not commit sensitive information
  2. Consider using environment variables for machine-specific settings
  3. Use conditional logic for OS-specific configurations
  4. Keep module installations separate from profile configurations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment