Last active
January 14, 2024 19:11
-
-
Save zbalkan/4997a2824fd4468aa74ee09510f5f091 to your computer and use it in GitHub Desktop.
My $PROFILE
This file contains hidden or 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
# Logging | |
$LogCommandHealthEvent = $true | |
$LogCommandLifecycleEvent = $true | |
# Culture | |
Set-Culture -CultureInfo en-us | |
[Console]::InputEncoding = [System.Text.Encoding]::UTF8 | |
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 | |
# Modify Get-History alias, like history command in bash | |
$OverrideHistoryAlias = $true | |
$HISTTIMEFORMAT = "dd/MM/yyyy HH:mm:ss" # Ref: https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings | |
if ($OverrideHistoryAlias) { | |
function historyWithTimestamp { | |
$h = Get-History | | |
Select-Object @{N = 'Order'; E = { $_.Id } }, | |
@{N = 'TimeStamp'; E = { $_.StartExecutionTime.ToString($HISTTIMEFORMAT, [CultureInfo]::InvariantCulture) } }, | |
@{N = 'Command'; E = { $_.Commandline } } | |
$h | more | |
} | |
Set-Alias -Name history -Value historyWithTimestamp -Option AllScope -Force -ErrorAction Ignore | |
} | |
else { | |
if ((Get-Alias -Name history).ResolvedCommand.Name -notlike 'Get-History') { | |
Set-Alias -Name history -Value Get-History -Option AllScope -Force -ErrorAction Ignore | |
} | |
} | |
# Check if started in terminal | |
$inTerminal = $false | |
if ($env:WT_SESSION) { | |
$inTerminal = $true | |
} | |
# Check if started in VSCode | |
$inVSCode = $false | |
if ($env:TERM_PROGRAM -eq 'vscode') { | |
$inVSCode = $true | |
} | |
# Check PowerShell version | |
$pwsh = $false | |
if ($PSVersionTable.PSVersion.Major -gt 5) { | |
$pwsh = $true | |
} | |
# Check git | |
$DefaultErrorActionPreference = $ErrorActionPreference | |
$ErrorActionPreference = 'SilentlyContinue' | |
# Check if git is installed | |
if($null -ne (Get-Command "git")){ | |
$gitInstalled = $true | |
Write-Output "Confirmed that git is installed." | |
} | |
else { | |
$gitInstalled = $false | |
} | |
# Check dotnet tools installed | |
if ($null -ne (Get-Command "dotnet")) { | |
$dotnetToolInstalled = $true | |
Write-Output "Confirmed that dotnet CLI tool is installed." | |
} | |
else { | |
$dotnetToolInstalled = $false | |
} | |
$ErrorActionPreference = $DefaultErrorActionPreference | |
if ($gitInstalled) { | |
# Import posh-git module to work with git | |
Import-Module posh-git | |
} | |
# OhMyPosh Console modification for Windows Terminal | |
if ($pwsh -and ($inTerminal -or $inVSCode)) { | |
# Apply Theme (Font: FiraCode NF) | |
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\rudolfs-dark.omp.json" | Invoke-Expression | |
Import-Module Terminal-Icons | |
Set-PSReadLineOption -PredictionSource History | |
Set-PSReadLineOption -EditMode Windows | |
} | |
# Useful shortcuts for traversing directories | |
function cd.. { Set-Location .. } | |
function cd... { Set-Location ..\.. } | |
function cd.... { Set-Location ..\..\.. } | |
# Compute file hashes - useful for checking successful downloads | |
function md5 { Get-FileHash -Algorithm MD5 $args } #DevSkim: ignore DS126858 | |
function sha1 { Get-FileHash -Algorithm SHA1 $args } #DevSkim: ignore DS126858 | |
function sha256 { Get-FileHash -Algorithm SHA256 $args } | |
# Quick shortcut to start notepad | |
function n { notepad $args } | |
# Quick shortcut to start Explorer on current directory | |
function w { explorer . } | |
# Quick shortcut to start micro CLI editor | |
function edit { micro $args } | |
# Call Powershell ISE | |
function ise { Start-Process powershell_ise.exe $args } | |
# Reload Powershell profile and clear the screen | |
function refprof { Write-Output "Reloading PowerShell Profile ($PROFILE)"; if (Test-Path $PROFILE) { . $PROFILE } else { Write-Warning "No profile found at path $PROFILE" } } | |
# Use man for Get-Help | |
function man { Get-Help -Name $args -Full } | |
# UNIX touch alternative | |
function touch { if ($args.Count -gt 0) { New-Item -ItemType File -Path $args | Out-Null } else { Write-Output "No arguments" } } | |
# py is python | |
function py { python.exe $args } | |
# Shorthand for UTC time | |
function utc { (Get-Date).ToUniversalTime() } | |
# Use ipconfig /all shortcut | |
function ip { ipconfig /all } | |
# Shutdown now | |
function shutdownnow { Stop-Computer -Force -Confirm:$false } | |
# What's my ip? Where I am? | |
function whatsmyip { $env:externalip = (Invoke-WebRequest "ifconfig.me/ip").Content; Write-Output $env:externalip } | |
function whereami { if ($null -eq $env:externalip) { whatsmyip | Out-Null } ; (Invoke-WebRequest "ipinfo.io/$(whatsmyip)/city").Content } | |
function weather { Invoke-RestMethod -Uri 'https://wttr.in/?format=4' } | |
# ln alternative. Symbolic link only | |
function ln ([string]$sourceFile, [string]$symbolicLink) { | |
New-Item -ItemType SymbolicLink -Path $sourceFile -Value $symbolicLink | Out-Null | |
} | |
# cat with mdcat capability | |
function smartcat { $path = $args[0]; if ($null -eq $path) { return }; if ($path -like "*.md") { mdcat $path; Write-Verbose "Opening MD file." } else { Get-Content $path ; Write-Verbose "Opening non-MD file." } } | |
Set-Alias -Name cat -Value smartcat -Option AllScope -Force -ErrorAction Ignore | Out-Null | |
# pip upgrade all | |
function pipupgrade { | |
Set-Variable ProgressPreference SilentlyContinue | |
$c = Test-NetConnection -WarningAction SilentlyContinue | |
$isConnected = $c.PingSucceeded | |
if ($isConnected) { | |
try { | |
python.exe -m pip install --upgrade pip | |
pip freeze | ForEach-Object { $_.split('==')[0] } | ForEach-Object { pip install --upgrade $_ } | |
} | |
catch { | |
return | |
} | |
} | |
else { | |
Write-Warning -Message "No internet connection." | |
} | |
} | |
# PowerShell upgrade all | |
function psupgrade { | |
Set-Variable ProgressPreference SilentlyContinue | |
$c = Test-NetConnection -WarningAction SilentlyContinue | |
$isConnected = $c.PingSucceeded | |
if ($isConnected) { | |
Get-Module -ListAvailable | ForEach-Object { Update-Module -AcceptLicense -Confirm:$false -ErrorAction Stop } | |
Try { Update-Help -ErrorAction Ignore } Catch [System.InvalidOperationException] { Write-Output "Some help files could not be updated. Run as administrator to update." } | |
} | |
else { | |
Write-Warning -Message "No internet connection." | |
} | |
} | |
# Drive shortcuts | |
function HKLM: { Set-Location HKLM: } | |
function HKCU: { Set-Location HKCU: } | |
function Env: { Set-Location Env: } | |
# Simple function to start a new elevated process. If arguments are supplied then | |
# a single command is started with admin rights; if not then a new admin instance | |
# of PowerShell is started. | |
function admin { | |
$exe = "$PSHOME\powershell.exe" | |
if ($pwsh) { $exe = "$PSHOME\pwsh.exe" } | |
# if($inTerminal) { $exe = "wt.exe -d ."} | |
Write-Output "Starting $exe as admin" | |
if ($args.Count -gt 0) { | |
$argList = "& '" + $args + "'" | |
Start-Process "$exe" -Verb runAs -ArgumentList $argList | |
} | |
else { | |
Start-Process "$exe" -Verb runAs | |
} | |
} | |
function funcListAll { Get-ChildItem -Force } | |
Set-Alias ll funcListAll | |
function funcReboot { Restart-Computer -Force -Confirm:$false } | |
Set-Alias reboot funcReboot | |
function winfetch { | |
& "D:\OneDrive\Documents\powershell\Scripts\pwshfetch-test-1.ps1" | |
} | |
# dotnet suggest shell start | |
if ($dotnetToolInstalled) { | |
if (Get-Command "dotnet-suggest" -ErrorAction SilentlyContinue) { | |
$availableToComplete = (dotnet-suggest list) | Out-String | |
$availableToCompleteArray = $availableToComplete.Split([Environment]::NewLine, [System.StringSplitOptions]::RemoveEmptyEntries) | |
Register-ArgumentCompleter -Native -CommandName $availableToCompleteArray -ScriptBlock { | |
param($wordToComplete, $commandAst, $cursorPosition) | |
$fullpath = (Get-Command $commandAst.CommandElements[0]).Source | |
$arguments = $commandAst.Extent.ToString().Replace('"', '\"') | |
dotnet-suggest get -e $fullpath --position $cursorPosition -- "$arguments" | ForEach-Object { | |
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) | |
} | |
} | |
} | |
else { | |
"Unable to provide System.CommandLine tab completion support unless the [dotnet-suggest] tool is first installed." | |
"See the following for tool installation: https://www.nuget.org/packages/dotnet-suggest" | |
} | |
$env:DOTNET_SUGGEST_SCRIPT_VERSION = "1.0.2" | |
# dotnet suggest script end | |
} | |
<# | |
.Synopsis | |
'more' command for a list of objects | |
.DESCRIPTION | |
Iterate over objects one by one. Use any key to iterate, and `Ctrl+C` to cancel. | |
.EXAMPLE | |
Step-InputObject -InputObject @(1,2,3,4,'a') | |
.EXAMPLE | |
@(1,2,3,4,'a') | iter | |
#> | |
function Step-InputObject | |
{ | |
[CmdletBinding()] | |
[Alias('iter')] | |
[OutputType([object])] | |
Param | |
( | |
# InputObject | |
[Parameter(Mandatory=$true, | |
ValueFromPipeline=$true, | |
ValueFromPipelineByPropertyName=$true, | |
Position=0)] | |
$InputObject | |
) | |
Process | |
{ | |
$InputObject | ForEach-Object { $PSItem; $null = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")} | |
} | |
} | |
Clear-Host |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment