Last active
March 28, 2024 14:57
-
-
Save chrisfcarroll/f3ecb2892f996149ee039d48abb57101 to your computer and use it in GitHub Desktop.
Common Aliases and Paths for PowerShell Profile Microsoft.PowerShell_profile.ps1
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://gist.github.com/chrisfcarroll/f3ecb2892f996149ee039d48abb57101 | |
# Aliases and Paths for PowerShell Profile ~\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 | |
# | |
# ----------------------------------------------------- | |
# For machines where you have no privileges you should be able to enable scripts for yourself only with: | |
# | |
# `Set-ExecutionPolicy RemoteSigned -Scope CurrentUser` | |
# ----------------------------------------------------- | |
# | |
# Sections: Paths, Editors, Tab Completion, Git abbreviations, Docker abbreviations, other prefs, firstRun | |
# | |
Param([switch]$firstRun) | |
# -- Paths ----------------------------------------------------- | |
$env:PATH="$env:PATH;$(Resolve-Path ~\.dotnet);(Resolve-Path ~\.dotnet\tools)" | |
if(test-path ~/.secrets.ps1){ . ~/.secrets.ps1 ; function editsecrets {edit (Resolve-Path ~/.secrets.ps1)} } | |
if(test-path ~/abbreviations.ps1){ . ~/abbreviations.ps1 ; function editabbreviations {edit (Resolve-Path ~/abbreviations.ps1)} } | |
# -- Editors ----------------------------------------------------- | |
if(Resolve-Path "C:\Program Files\Vim\vim90\vim.exe"){$global:EDITOR="C:\Program Files\Vim\vim90\vim.exe"} | |
if(Get-Command code -ea SilentlyContinue) | |
{ | |
New-Alias edit code | |
} | |
elseif (Get-Command code-insiders -ea SilentlyContinue) | |
{ | |
new-alias code code-insiders | |
New-Alias edit code-insiders | |
} | |
elseif(Get-Command subl -ea SilentlyContinue) | |
{ | |
New-Alias edit subl | |
} | |
elseif(Test-Path ~/Applications/sublime_text_build_4126_x64/subl.exe /*Update as appropriate*/ ) | |
{ | |
function subl{ ~/Applications/sublime_text_build_4126_x64/subl.exe $args } | |
New-Alias edit subl | |
} | |
function editprofile { edit $PROFILE } | |
if((Resolve-Path "C:\Program Files\Vim\vim90\vim.exe") -and -not (Get-Command vim -ea SilentlyContinue)) | |
{ | |
New-Alias vim "C:\Program Files\Vim\vim90\vim.exe" | |
New-Alias vi "C:\Program Files\Vim\vim90\vim.exe" | |
} | |
# ----------------------------------------------------- | |
# Autocompletion vvv | |
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete | |
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward | |
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward | |
# Autocompletion ^^^ | |
# ----------------------------------------------------- | |
# Git vvv | |
Write-Host "Use source command '. pg' to load poshgit" | |
function pg { | |
$stopwatchPG=[System.Diagnostics.Stopwatch]::StartNew() | |
Import-Module posh-git -ErrorAction SilentlyContinue | |
"PoshGit took $($stopwatchPG.ElapsedMilliseconds)ms" | |
if($?){ | |
$GitPromptSettings.DefaultPromptAbbreviateHomeDirectory = $true | |
function Get-ShorterPromptPath { | |
(Get-Location).ToString().Replace("$env:USERPROFILE","~") ` | |
-replace '([^\\]{4})([^\\]{4,})\\','$1…\' ` | |
-replace '\\([^\\]{6})([^\\]{3,})([^\\]{5})$','\$1…$3' | |
} | |
$GitPromptSettings.DefaultPromptPath.Text = '$(Get-ShorterPromptPath)' | |
function nll { if(-not $GitPromptSettings.AfterStatus.Text -match "\n"){$GitPromptSettings.AfterStatus.Text += "`n" }} | |
}else{ | |
function prompt { | |
$d=(Get-Location).ToString().Replace("$env:USERPROFILE","~") ` | |
-replace '([^\\]{1})([^\\]{5,})\\','$1..\' ` | |
-replace '\\([^\\]{4})([^\\]{5,})([^\\]{4})$','\$1..$3' | |
"$d> " | |
} | |
} | |
} | |
function gaa { git add $args :/ } | |
function gb { git branch $args } | |
function gbr { git branch --remote $args } | |
function gco { git checkout $args} | |
function gitcom { git commit -m "$args"} | |
function gitacom { git commit -a -m "$args"} | |
function gitaaacom { git add :/ ; git commit -m "$args"} | |
function gdiff { git diff $args } | |
function glog { git log ` | |
--pretty=format:'%Cred%h%Creset %C(yellow)%d%Creset %s %Cgreen%cd %C(bold blue)%an%Creset' ` | |
--abbrev-commit --date=format:'%d-%m-%Y %H:%M' $args } | |
function glogg { glog --graph $args } | |
function glog1 {git log --pretty=oneline $args} | |
function gs { git status $args } | |
function gpull { git pull $args} | |
function gpush { git push $args} | |
function gpullpush { | |
git remote update | |
if( ($s=(git status | select-string -Pattern "Your branch is behind|have diverged")).Count ) { | |
$s | |
git pull | |
if( $s -match "can be fast-forwarded.}Successfully rebased and updated"){ | |
"Pushing ..." | |
git push | |
} | |
} else { | |
"Pushing ..." | |
git push | |
} | |
} | |
function openremote { git remote -v | select-string "https://[^ ]+" | Select-Object -First 1 | %{ Start-Process "$($_.Matches.Value)?version=" } } | |
#Git ^^^ | |
# ----------------------------------------------------- | |
#Docker vvv | |
function dockerdesktop { Start-Process "C:\Program Files\Docker\Docker\Docker Desktop.exe" } | |
function ddls-all { docker container ls -aq } | |
function ddstop-all { docker container stop $(docker container ls -aq) } | |
function ddrm-all { docker container rm $(docker container ls -aq) } | |
#Docker ^^^ | |
# ----------------------------------------------------- | |
#Console Colours for the PowerShell White or Yellow on Blue theme vvv | |
$Host.PrivateData.ErrorForegroundColor = "Yellow" | |
$Host.PrivateData.ErrorBackgroundColor = $Host.UI.RawUI.BackgroundColor | |
$Host.PrivateData.WarningForegroundColor = "DarkYellow" | |
$Host.PrivateData.WarningBackgroundColor = $Host.UI.RawUI.BackgroundColor | |
#Console Colours ^^^ | |
# ----------------------------------------------------- | |
#Other preferences vvvv | |
$global:repos= (Resolve-Path ~/repos) | |
$global:deploy= (Resolve-Path ~/deploy) | |
$global:scratch= (Resolve-Path ~/scratch) | |
function cdr { Set-Location $global:repos} | |
function cds { Set-Location $global:scratch} | |
function cddeploy { Set-Location $global:deploy} | |
new-alias gh get-help | |
new-alias ll Get-ChildItem | |
new-alias open start | |
function ss { start-process "https://bing.com/search?q=$([uri]::EscapeDataString($args))" } | |
function pss { start-process "https://bing.com/search?q=$([uri]::EscapeDataString("powershell $args"))" } | |
new-alias wh Get-Command # which.exe,where.exe,get-command are each subtly different | |
function wh { $r= Get-Command $args ; $r; if($r.GetType() -eq [System.Management.Automation.FunctionInfo]){ $r.ScriptBlock } } | |
if([System.Environment]::OSVersion.Platform -like 'Win*') | |
{ | |
if(Resolve-Path "$global:onedrive\Commands\AutohotkeyStartup PC Keyboard.exe"){ | |
new-alias ahwin (Resolve-Path "$global:onedrive\Commands\AutohotkeyStartup PC Keyboard.exe") | |
} | |
if(Resolve-Path "$global:onedrive\Commands\AutohotkeyStartupForMacKeyboard.exe"){ | |
new-alias ahmac (Resolve-Path "$global:onedrive\Commands\AutohotkeyStartupForMacKeyboard.exe") | |
} | |
function pgrep([Parameter(Position=0,mandatory=$true)][string]$Name, [switch]$full ) | |
{ | |
Get-WmiObject Win32_Process ` | |
| Where-Object { $_.Name -match $Name -or ( $full -and ($_.CommandLine -match $Name)) } ` | |
| Select-Object -Property ProcessId, Name, CommandLine, Status | |
} | |
} | |
#Other preferences ^^^ | |
# ----------------------------------------------------- | |
# ----------------------------------------------------- | |
#Work Specific vvvv | |
function publish-to-iis($fromSolutionDirectory, $toIISAppPath, $publishProfileXmlFileNameNoPath, $buildConfiguration, [switch]$noConfirm) | |
{ | |
if(-not "$fromSolutionDirectory" -or -not "$toIISAppPath" ` | |
-or -not "$publishProfileXmlFileNameNoPath" -or -not "$buildConfiguration"){ | |
Write-Host "Usage: publish-to-iis `$fromSolutionDirectory, `$toIISAppPath, `$publishProfileXmlFileNameNoPath, $buildConfiguration" | |
break | |
} | |
Get-ChildItem $fromSolutionDirectory/$publishProfileXmlFileNameNoPath -recurse | |
Push-Location $fromSolutionDirectory | |
if(Test-Path $toIISAppPath\web.config){ | |
Remove-Item $toIISAppPath\web.config -Verbose | |
}else{ | |
Write-Warning "target web.config doesn't exist did you already delete?" | |
if(-not $noConfirm){ | |
while( -not ( ($choice= (Read-Host "Press Y to continue or N to stop:")) -match "^(Y|N)$")){ "Y or N ?"} | |
if($choice -ne 'y'){return} | |
} | |
} | |
$publishProfileXmlFileNameNoPath= Split-Path $publishProfileXmlFileNameNoPath -Leaf | |
"Removing directories..." | |
Get-ChildItem $toIISAppPath\* -Directory | | |
ForEach-Object{ $_.Name ; Remove-Item $_ -recurse } | |
Get-ChildItem $toIISAppPath | |
msbuild /p:Configuration=$buildConfiguration /p:DeployOnBuild=true /p:PublishProfile=$publishProfileXmlFileNameNoPath | |
Pop-Location | |
} | |
function yesno { | |
while( -not ( ($choice= (Read-Host "May I continue?")) -match "^(ye?s?|no?)$")){ "Y or N ?"} | |
return ($choice -match "^y") | |
} | |
#Work Specific ^^^ | |
# ----------------------------------------------------- | |
if($firstRun) | |
{ | |
git config --global alias.root 'rev-parse --show-toplevel' | |
git config --global alias.lg "log --color --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --graph" | |
git config --global alias.lg1 "log --color --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" | |
if((Get-Command p4merge) -and (Test-Path (Get-Command p4merge).Source)) | |
{ | |
git config --global merge.tool p4merge | |
git config --global diff.tool p4merge | |
} | |
else{Write-Warning "p4merge not found"} | |
Install-Module -Name posh-git -Scope CurrentUser | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment