Skip to content

Instantly share code, notes, and snippets.

@vanuyshka
Created November 5, 2023 06:52
Show Gist options
  • Save vanuyshka/89e87b8bb6e8d7eaba66465ee8090ff3 to your computer and use it in GitHub Desktop.
Save vanuyshka/89e87b8bb6e8d7eaba66465ee8090ff3 to your computer and use it in GitHub Desktop.
PowerShell Profile
# Git
# Install-Module -Name Posh-Git -Scope CurrentUser -Force
# Install-Module -Name PSScriptAnalyzer -Scope CurrentUser -Force
Import-Module Posh-Git
function Edit-Profile { code $PROFILE.CurrentUserAllHosts }
function Get-CmdletAlias ($cmdletname) {
Get-Alias |
Where-Object -FilterScript { $_.Definition -like "$cmdletname" } |
Format-Table -Property Definition, Name -AutoSize
}
function prompt {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = [Security.Principal.WindowsPrincipal] $identity
$adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator
$GitPromptSettings.DefaultPromptPath.ForegroundColor = 0xFFA500
$GitPromptSettings.DefaultPromptBeforeSuffix.Text = [System.Environment]::NewLine
$ESC = [char]27
$prompt = $(if (Test-Path variable:/PSDebugContext) { "[DBG]: " }
elseif ($principal.IsInRole($adminRole)) { "[ADMIN]: " }
else { '' }
)
$prompt += "$ESC[92m" + $env:COMPUTERNAME + "$ESC[0m "
# $K8sContext=$(Get-Content ~/.kube/config | grep "current-context:" | sed "s/current-context: //")
$K8sContext = $((Get-Content ~/.kube/config) -match "current-context:" -replace "current-context: ")
If ($K8sContext) {
$prompt += "$ESC[95m" + $K8sContext + "$ESC[0m "
}
$prompt += & $GitPromptScriptBlock
if ($prompt) { "$prompt" } else { "> " }
}
function md5 { Get-FileHash -Algorithm MD5 $args }
function sha1 { Get-FileHash -Algorithm SHA1 $args }
function sha256 { Get-FileHash -Algorithm SHA256 $args }
function fnd {
if ($args.Count -gt 0) {
Get-ChildItem -Recurse -Include "$args" | Foreach-Object FullName
}
else {
Get-ChildItem -Recurse | Foreach-Object FullName
}
}
function admin { Start-Process "$psHome\pwsh.exe" -Verb runAs }
# function admin { Start-Process "Start-Process -Verb RunAs cmd.exe '/c start wt.exe'" -Verb runAs }
Set-Alias -Name su -Value admin
Set-Alias -Name sudo -Value admin
New-Alias open Invoke-Item
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
Set-PSReadLineOption -PredictionViewStyle ListView
if (Get-Command "winget.exe" -ErrorAction SilentlyContinue) {
Register-ArgumentCompleter -Native -CommandName winget -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
[Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.Utf8Encoding]::new()
$Local:word = $wordToComplete.Replace('"', '""')
$Local:ast = $commandAst.ToString().Replace('"', '""')
winget complete --word="$Local:word" --commandline "$Local:ast" --position $cursorPosition | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
}
if (Get-Command "op.exe" -ErrorAction SilentlyContinue) { op completion powershell | Out-String | Invoke-Expression }
if (Get-Command "kubectl.exe" -ErrorAction SilentlyContinue) {
kubectl completion powershell | Out-String | Invoke-Expression
New-Alias -Name k -Value kubectl.exe
function kcuc { kubectl config use-context $args }
}
if (Get-Command "helm.exe" -ErrorAction SilentlyContinue) { helm completion powershell | Out-String | Invoke-Expression }
if (Get-Command "gh.exe" -ErrorAction SilentlyContinue) { Invoke-Expression -Command $(gh completion -s powershell | Out-String) }
if (Get-Command "aws.exe" -ErrorAction SilentlyContinue) {
Register-ArgumentCompleter -Native -CommandName aws -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
$env:COMP_LINE = $wordToComplete
if ($env:COMP_LINE.Length -lt $cursorPosition) {
$env:COMP_LINE = $env:COMP_LINE + " "
}
$env:COMP_POINT = $cursorPosition
aws_completer.exe | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
Remove-Item Env:\COMP_LINE
Remove-Item Env:\COMP_POINT
}
}
if (Get-Command "az.exe" -ErrorAction SilentlyContinue) {
Register-ArgumentCompleter -Native -CommandName az -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
$completion_file = New-TemporaryFile
$env:ARGCOMPLETE_USE_TEMPFILES = 1
$env:_ARGCOMPLETE_STDOUT_FILENAME = $completion_file
$env:COMP_LINE = $wordToComplete
$env:COMP_POINT = $cursorPosition
$env:_ARGCOMPLETE = 1
$env:_ARGCOMPLETE_SUPPRESS_SPACE = 0
$env:_ARGCOMPLETE_IFS = "`n"
$env:_ARGCOMPLETE_SHELL = 'powershell'
az 2>&1 | Out-Null
Get-Content $completion_file | Sort-Object | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, "ParameterValue", $_)
}
Remove-Item $completion_file, Env:\_ARGCOMPLETE_STDOUT_FILENAME, Env:\ARGCOMPLETE_USE_TEMPFILES, Env:\COMP_LINE, Env:\COMP_POINT, Env:\_ARGCOMPLETE, Env:\_ARGCOMPLETE_SUPPRESS_SPACE, Env:\_ARGCOMPLETE_IFS, Env:\_ARGCOMPLETE_SHELL
}
}
function cdp { Set-Location -Path ~\Projects }
New-Alias -Name ll -Value Get-ChildItem
# Completion for SSH
function Get-SSHHost($sshConfigPath) {
Get-Content -Path $sshConfigPath `
| Select-String -Pattern '^Host ' `
| ForEach-Object { $_ -replace 'Host ', '' } `
| ForEach-Object { $_ -split ' ' } `
| Sort-Object -Unique `
| Select-String -Pattern '^.*[*!?].*$' -NotMatch
}
Register-ArgumentCompleter -CommandName 'ssh', 'scp', 'sftp' -Native -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
$sshPath = "$env:USERPROFILE\.ssh"
$hosts = Get-Content -Path "$sshPath\config" `
| Select-String -Pattern '^Include ' `
| ForEach-Object { $_ -replace 'Include ', '' } `
| ForEach-Object { Get-SSHHost "$sshPath/$_" }
$hosts += Get-SSHHost "$sshPath\config"
$hosts = $hosts | Sort-Object -Unique
$hosts | Where-Object { $_ -like "$wordToComplete*" } `
| ForEach-Object { $_ }
}
# PowerShell parameter completion shim for the dotnet CLI
if (Get-Command "dotnet.exe" -ErrorAction SilentlyContinue) {
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
}
$env:CLOUDSDK_PYTHON_SITEPACKAGES = "1"
$env:DOTNET_CLI_TELEMETRY_OPTOUT = "1"
# https://stackoverflow.com/questions/28682642/powershell-why-is-using-invoke-webrequest-much-slower-than-a-browser-download
$ProgressPreference = 'SilentlyContinue'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment