Last active
May 20, 2024 06:50
-
-
Save niktekusho/5585958165523011531607e37ad8fc53 to your computer and use it in GitHub Desktop.
My PowerShell Profile contains mostly aliases to default Windows cmds and some custom functions I use often.
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
# niktekusho's profile! | |
# Put this file in ${HOME}\Documents\PowerShell\ | |
# Custom environment vars | |
# Persist workspace env-var | |
[System.Environment]::SetEnvironmentVariable('GH_PROJECTS_PATH', "$env:HOME\Documents\Github") | |
# Custom prompt | |
function prompt { | |
$baseRegexes = @{ | |
$HOME = '~'; | |
"${env:GH_PROJECTS_PATH}" = 'gh'; | |
} | |
$currentPath = $executionContext.SessionState.Path.CurrentLocation.Path | |
$resolvedPath = $currentPath | |
# Replace every key in the baseRegexes dictionary with the associated value (if the base pattern matches) | |
foreach ($key in $baseRegexes.Keys) { | |
$regex = [regex]::Escape($key) + "(\\.*)*$" | |
if ($currentPath -match $regex) { | |
$replacement = $baseRegexes[$key] | |
$resolvedPath = $($resolvedPath -replace $regex, ($replacement + '$1')) | |
} | |
} | |
# $resolvedPath = $($currentPath -replace $regex, '~$1') | |
Write-Host "$ " -NoNewline | |
Write-Host "$resolvedPath" -ForegroundColor DarkYellow -NoNewline | |
Write-Host " > " -ForegroundColor Red -NoNewline | |
return " " | |
} | |
# Shortcut to the "WORKSPACES" env var (path) | |
function Enter-Workspaces { | |
if ($env:WORKSPACES) { | |
Set-Location $env:WORKSPACES | |
} | |
} | |
Set-Alias ws Enter-Workspaces | |
# Shortcut to my GitHub projects | |
function Enter-GitHubProjects { | |
Set-Location $env:GH_PROJECTS_PATH | |
} | |
Set-Alias ghb Enter-GitHubProjects -Option ReadOnly | |
# Useful aliases | |
# Set-Alias mkdir make-dir -Option ReadOnly | |
# Set-Alias rm del-cli -Option AllScope, ReadOnly | |
Set-Alias np ${env:windir}\notepad.exe | |
# Shortcuts to optimize Docker on Windows VM virtual disk | |
function Invoke-DockerOptimize([String] $mode = "Quick") { | |
Write-Output "Running 'Docker Optimize' function in $mode mode." | |
return Optimize-VHD -Path "C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\MobyLinuxVM.vhdx" -Mode $mode | |
} | |
# Simple alias of the function above | |
Set-Alias dockerqopt Invoke-DockerOptimize -Option ReadOnly | |
# "Alias" of the function above but with "Full" parameter | |
function dockerfullopt { | |
return Invoke-DockerOptimize -mode "Full" | |
} | |
# Similar to the time command in UNIX envs. "time" is already taken though... | |
Set-Alias timeit Measure-Command -Option ReadOnly | |
# Shortcut to open explorer on current dir | |
function Open-ExplorerCurrentDir { | |
explorer . | |
} | |
Set-Alias e Open-ExplorerCurrentDir -Option ReadOnly | |
# Shortcut to open the PowerShell profile in a text editor | |
function Open-PSProfileFile { | |
# Use Visual Studio Code if it is installed, otherwise fallback to notepad | |
if (Get-Command "code" -ErrorAction SilentlyContinue) { | |
code $PROFILE.CurrentUserAllHosts | |
} else { | |
notepad $PROFILE.CurrentUserAllHosts | |
} | |
} | |
Set-Alias psprof Open-PSProfileFile -Option ReadOnly | |
# Unix shell functions | |
function which($cmd) { (Get-Command $cmd).Definition } | |
function whoami { "$(Get-Content Env:\USERDOMAIN)/$(Get-Content Env:\USERNAME)" } | |
function touch($fileName) { Out-File -FilePath "$(Get-Location)/${fileName}" } | |
# Choco helpers | |
function Invoke-ChocoUpgradeAll { | |
param ( | |
[switch]$dryRun = $false | |
) | |
# Run only if choco is installed | |
if (Get-Command "choco" -ErrorAction SilentlyContinue) { | |
$noop = $null | |
if ($dryRun) { | |
Write-Output 'Running "choco" in dry-run mode.' | |
$noop = '--noop' | |
} | |
choco upgrade all -y $noop | |
} else { | |
Write-Warning 'You do not have "choco" installed (or in your $PATH)' | |
} | |
} | |
Set-Alias chocoup Invoke-ChocoUpgradeAll -Option ReadOnly | |
# Shortcut to change local user in a git repository | |
function Invoke-ChangeGitUser([String] $username = "niktekusho", [String] $email = "[email protected]", [switch] $dryrun = $False) { | |
if ($dryrun) { | |
Write-Output "Running 'Change Git User' function in dryrun mode." | |
Write-Output "git config user.name $username" | |
Write-Output "git config user.email $email" | |
} else { | |
Write-Output "User: $username | Email: $email" | |
git config user.name $username | |
git config user.email $email | |
} | |
} | |
# Refresh Path env var in current terminal session | |
function Refresh-Path() { | |
# User before Machine to allow for "user-defined" Path override. | |
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","User") + ";" + [System.Environment]::GetEnvironmentVariable("Path","Machine") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment