Last active
July 13, 2024 06:54
-
-
Save akunzai/3af259df54dcdd6073293e6f8efe8dbf to your computer and use it in GitHub Desktop.
My PowerShell profile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_profiles | |
# PowerShell < 6 on Windows: $Home\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 | |
# PowerShell >= 6 on Windows: $Home\Documents\PowerShell\Microsoft.PowerShell_profile.ps1 | |
# PowerShell on Linux/macOS: ~/.config/powershell/Microsoft.PowerShell_profile.ps1 | |
# https://learn.microsoft.com/dotnet/core/tools/dotnet-environment-variables#dotnet_cli_ui_language | |
$env:DOTNET_CLI_UI_LANGUAGE = 'en-us' | |
# https://github.com/PowerShell/PSReadLine | |
if (Get-Command 'Set-PSReadlineKeyHandler' -ErrorAction SilentlyContinue) { | |
# Bind the Ctrl+D key to exit the PowerShell | |
Set-PSReadlineKeyHandler -Chord ctrl+d -Function ViExit -ErrorAction SilentlyContinue | |
# Bind the Ctrl+W key to delete word before cursor | |
Set-PSReadlineKeyHandler -Chord ctrl+w -Function BackwardDeleteWord | |
# Bind the Ctrl+E key to move cursor to the end of line | |
Set-PSReadlineKeyHandler -Chord ctrl+e -Function EndOfLine | |
# Bind the Ctrl+A key to move cursor to the begin of line | |
Set-PSReadlineKeyHandler -Chord ctrl+a -Function BeginningOfLine | |
} | |
if (Get-Command 'Set-PSReadLineOption' -ErrorAction SilentlyContinue) { | |
if ((Get-Module PSReadLine).Version -ge [System.Version]"2.1.0") { | |
# Enable Predictive IntelliSense | |
Set-PSReadLineOption -PredictionSource History | |
} | |
# https://github.com/dracula/powershell | |
Set-PSReadlineOption -Color @{ | |
"Command" = [ConsoleColor]::Green | |
"Parameter" = [ConsoleColor]::Gray | |
"Operator" = [ConsoleColor]::Magenta | |
"Variable" = [ConsoleColor]::White | |
"String" = [ConsoleColor]::Yellow | |
"Number" = [ConsoleColor]::Blue | |
"Type" = [ConsoleColor]::Cyan | |
"Comment" = [ConsoleColor]::DarkCyan | |
} | |
} | |
if ($PSVersionTable.PSVersion.Major -lt 6) { | |
# Passing output between PowerShell cmdlets | |
# https://stackoverflow.com/questions/40098771/changing-powershells-default-output-encoding-to-utf-8 | |
$PSDefaultParameterValues['*:Encoding'] = 'utf8' | |
# Passing output from PowerShell to Native Application | |
# https://gist.github.com/xoner/4671514 | |
$OutputEncoding = [console]::OutputEncoding = [Text.UTF8Encoding]::UTF8 | |
# Enable tls1.2 from default (Ssl3, Tls) | |
# https://stackoverflow.com/questions/41618766/powershell-invoke-webrequest-fails-with-ssl-tls-secure-channel | |
[Net.ServicePointManager]::SecurityProtocol = 'tls12, tls11, tls' | |
# Progress bar can significantly impact cmdlet performance | |
# https://github.com/PowerShell/PowerShell/issues/2138 | |
$ProgressPreference = 'SilentlyContinue' | |
# Measure-Command { Invoke-WebRequest -UseBasicParsing "http://ipv4.download.thinkbroadband.com/10MB.zip" } | |
} | |
# https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_prompts | |
function prompt { | |
$suffix = '$'; | |
if (($IsWindows -or [Environment]::OSVersion.Platform -eq [PlatformID]::Win32NT) -and ` | |
([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole( ` | |
[Security.Principal.WindowsBuiltInRole]::Administrator)) { | |
$suffix = '#'; | |
} | |
if (Test-Path variable:/PSDebugContext) { | |
Write-Host 'DBG: ' -ForegroundColor Blue -NoNewline | |
} | |
Write-Host "$($executionContext.SessionState.Path.CurrentLocation) " -ForegroundColor Yellow -NoNewline | |
Write-Host "| $(Get-Date -Format 'HH:mm')" -ForegroundColor Green | |
return "$suffix " | |
} | |
function rgit() { | |
$params = $args; | |
Get-ChildItem -Recurse -Directory -Force -Filter .git | ForEach-Object { | |
Write-Output "$($_.Parent.FullName): git $params"; | |
git -C $_.Parent.FullName $params; | |
} | |
} | |
# https://docs.gitignore.io/install/command-line | |
function gig { | |
param( | |
[Parameter(Mandatory = $true)] | |
[string[]]$list | |
) | |
$params = ($list | ForEach-Object { [uri]::EscapeDataString($_) }) -join "," | |
Invoke-WebRequest -Uri "https://www.toptal.com/developers/gitignore/api/$params" | Select-Object -ExpandProperty content | Out-File -FilePath $(Join-Path -path $pwd -ChildPath ".gitignore") -Encoding ascii | |
} | |
function Remove-OldModules { | |
$modules = Get-InstalledModule | |
foreach ($module in $modules) { | |
Write-Verbose -Message "Uninstalling old versions of $($module.Name) [latest is $( $module.Version)]" -Verbose | |
Get-InstalledModule -Name $module.Name -AllVersions | Where-Object {$_.Version -ne $module.Version} | Uninstall-Module -Verbose | |
} | |
} | |
if ($IsWindows -or [Environment]::OSVersion.Platform -eq [PlatformID]::Win32NT) { | |
if ((Get-Command 'podman' -ErrorAction SilentlyContinue) -and !(Get-Command 'docker' -ErrorAction SilentlyContinue)) { | |
Set-Alias docker podman | |
} | |
$gitBaseDir = "$env:UserProfile\AppData\Local\Programs\Git" | |
if (-not (Test-Path $gitBaseDir -ErrorAction SilentlyContinue)) { | |
$gitBaseDir = "$env:ProgramFiles\Git" | |
} | |
$gitLess = "$gitBaseDir\usr\bin\less.exe" | |
if (!(Get-Command 'less' -ErrorAction SilentlyContinue) -and (Test-Path $gitLess -ErrorAction SilentlyContinue)) { | |
Set-Alias less $gitLess -Option AllScope | |
} | |
$gitGrep = "$gitBaseDir\usr\bin\grep.exe" | |
if (!(Get-Command 'grep' -ErrorAction SilentlyContinue) -and (Test-Path $gitGrep -ErrorAction SilentlyContinue)) { | |
Set-Alias grep $gitGrep -Option AllScope | |
} | |
$scoopCurl = "$env:UserProfile\scoop\shims\curl.exe" | |
$gitCurl = "$gitBaseDir\mingw64\bin\curl.exe" | |
if (Test-Path $scoopCurl -ErrorAction SilentlyContinue) { | |
Set-Alias curl $scoopCurl -Option AllScope | |
} | |
elseif (Test-Path $gitCurl -ErrorAction SilentlyContinue) { | |
Set-Alias curl $gitCurl -Option AllScope | |
} | |
$scoopOpenSsl = "$env:UserProfile\scoop\apps\openssl\current\bin\openssl.exe" | |
$gitOpenSsl = "$gitBaseDir\usr\bin\openssl.exe" | |
if (Test-Path $scoopOpenSsl -ErrorAction SilentlyContinue) { | |
Set-Alias openssl $scoopOpenSsl -Option AllScope | |
} | |
elseif (Test-Path $gitOpenSsl -ErrorAction SilentlyContinue) { | |
Set-Alias openssl $gitOpenSsl -Option AllScope | |
} | |
# https://github.com/gerardog/gsudo | |
if (Get-Command 'gsudo.exe' -ErrorAction SilentlyContinue) { | |
Set-Alias sudo 'gsudo.exe' -Option AllScope | |
} | |
# https://docs.memurai.com/en/memurai-cli | |
if (Get-Command 'memurai-cli.exe' -ErrorAction SilentlyContinue) { | |
Set-Alias redis-cli 'memurai-cli.exe' -Option AllScope | |
} | |
# Make sure all chrome.exe processes are killed before running the command | |
function Start-Chrome { | |
$chrome = "${env:ProgramFiles}\Google\Chrome\Application\chrome.exe" | |
if (-not (Test-Path $chrome -ErrorAction SilentlyContinue)) { | |
$chrome = "${env:ProgramFiles(x86)}\Google\Chrome\Application\chrome.exe" | |
} | |
& $chrome $args | |
} | |
Set-Alias chrome Start-Chrome | |
function up { | |
Update-Module | |
# https://scoop.sh/ | |
if (Get-Command 'scoop' -ErrorAction SilentlyContinue) { | |
scoop update; | |
scoop update --all; | |
scoop cleanup --all --cache; | |
} | |
# https://github.com/microsoft/winget-cli | |
if (Get-Command 'winget' -ErrorAction SilentlyContinue) { | |
winget upgrade; | |
} | |
} | |
function touch { | |
Param( | |
[Parameter(Mandatory = $true)] | |
[string]$Path | |
) | |
if (Test-Path -LiteralPath $Path -ErrorAction SilentlyContinue) { | |
(Get-Item -Path $Path).LastWriteTime = Get-Date | |
} | |
else { | |
New-Item -Type File -Path $Path | |
} | |
} | |
# https://github.com/Microsoft/vswhere/wiki/Start-Developer-Command-Prompt | |
$vsWhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" | |
if (Test-Path $vsWhere -ErrorAction SilentlyContinue) { | |
# https://intellitect.com/enter-vsdevshell-powershell/ | |
# https://devblogs.microsoft.com/visualstudio/say-hello-to-the-new-visual-studio-terminal/ | |
function Start-VsDevShell { | |
$vsInstallPath = & $vsWhere -latest -property installationPath | |
$vsDevShellModule = Join-Path $vsInstallPath "Common7\Tools\Microsoft.VisualStudio.DevShell.dll" | |
Import-Module $vsDevShellModule -ErrorAction SilentlyContinue | |
Enter-VsDevShell -VsInstallPath "$vsInstallPath" -SkipAutomaticLocation | |
} | |
Set-Alias vsdevshell Start-VsDevShell | |
} | |
$subl = "${env:ProgramFiles}\Sublime Text\sublime_text.exe"; | |
if (Test-Path $subl -ErrorAction SilentlyContinue) { | |
function subl { &$subl $args } | |
} | |
# https://github.com/ForkIssues/TrackerWin/issues/416 | |
$fork = "${env:LocalAppData}\Fork\Fork.exe" | |
if (Test-Path $fork) { | |
function fork { | |
$path = if ($args.Count -gt 0) { $args[0] } else { '.' } | |
# Fork requires an absolute path | |
&$fork (Resolve-Path $path) | |
} | |
} | |
# https://ohmyposh.dev/ | |
if (Get-Command 'oh-my-posh.exe' -ErrorAction SilentlyContinue) { | |
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\dracula.omp.json" | Invoke-Expression | |
Enable-PoshTransientPrompt | |
} | |
} | |
$profileHomes = @( | |
$PSScriptRoot, | |
(Join-Path $PSScriptRoot $([Environment]::OSVersion.Platform))) | |
if ($env:USERPROFILE) { | |
$profileHomes += $env:USERPROFILE; | |
$profileHomes += (Join-Path $env:USERPROFILE $([Environment]::OSVersion.Platform)) | |
} | |
$stopWatch = [System.Diagnostics.Stopwatch]::new() | |
foreach ($profileHome in $profileHomes) { | |
$profilePath = Join-Path $profileHome "profile.ps1"; | |
if (Test-Path $profilePath -ErrorAction SilentlyContinue) { | |
$stopWatch.Restart() | |
. $profilePath | |
Write-Host "Loading $profilePath took $($stopWatch.ElapsedMilliseconds)ms." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment