Created
February 18, 2024 16:37
-
-
Save Abhinav1217/bf42ed4c06ac374d2168c8ceb247433b to your computer and use it in GitHub Desktop.
a basic $PROFILE for powershell
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
# Define an alias | |
# | |
# Set-Alias -Name backup-svn -Value { | |
# Param( | |
# [Alias('S')] | |
# [Parameter(Mandatory = $true)] | |
# [string] $SourcePath, | |
# [Alias('D')] | |
# [Parameter(Mandatory = $true)] | |
# [string] $DestinationPath | |
# ) | |
# | |
# $ExcludePatterns = @("*.svn*", "obj", "bin") | |
# Get-ChildItem -Path $SourcePath -File -Recurse -Exclude $ExcludePatterns | | |
# Compress-Archive -DestinationPath $DestinationPath | |
# } | |
# Define an alias | |
# Set-Alias -Name backup-svn -Value { | |
# Param( | |
# [Alias('S')] | |
# [Parameter(Mandatory = $true)] | |
# [string] $SourcePath, | |
# [Alias('D')] | |
# [Parameter(Mandatory = $true)] | |
# [string] $DestinationPath | |
# ) | |
# | |
# $ExcludePatterns = @("*.svn*", "obj", "bin") | |
# $FilesToCompress = Get-ChildItem -Path $SourcePath -File -Recurse -Exclude $ExcludePatterns | |
# Compress-Archive -Path $FilesToCompress.FullName -DestinationPath $DestinationPath | |
# } | |
# | |
#region Setup and Import | |
# Check if PSReadLine module is available | |
if (-not (Get-Module -Name PSReadLine -ListAvailable)) { | |
# Install the PSReadLine module | |
Install-Module -Name PSReadLine -Scope CurrentUser -Force -AllowClobber | |
} | |
# Import the PSReadLine module | |
Import-Module PSReadLine | |
# PSUtil - https://github.com/PowershellFrameworkCollective/PSUtil | |
# Check if PSUtil module is available | |
if (-not (Get-Module -Name PSUtil -ListAvailable)) { | |
# Install the PSUtil module | |
Install-Module -Name PSUtil -Scope CurrentUser -Force -AllowClobber | |
} | |
# Import the PSUtil module | |
Import-Module PSUtil | |
# BurntToast - https://github.com/Windos/BurntToast | |
# Check if BurntToast module is available | |
if (-not (Get-Module -Name BurntToast -ListAvailable)) { | |
# Install the BurntToast module | |
Install-Module -Name BurntToast -Scope CurrentUser -Force -AllowClobber | |
} | |
# Import the BurntToast module | |
Import-Module BurntToast | |
# SimplySql - https://github.com/mithrandyr/SimplySql | |
# Check if SimplySql module is available | |
if (-not (Get-Module -Name SimplySql -ListAvailable)) { | |
# Install the SimplySql module | |
Install-Module -Name SimplySql -Scope CurrentUser -Force -AllowClobber | |
} | |
# Import the SimplySql module | |
Import-Module SimplySql | |
#endregion | |
Function Grep-History { | |
Get-History | Where-Object CommandLine -like "*" | |
} | |
# Search command history for a perticular command text snippet. | |
Function Grep-History { | |
# Default value if | |
param ( [string]$Text = "*" ) | |
Get-History | Where-Object CommandLine -like "*$Text*" | |
} | |
#region Persistant command history in Powershell (Because this poor guy doesn't have any) | |
# $historyFile = "$HOME/.local/share/powershell/PSReadLine/consolehost_history.txt" | |
$historyFile = "C:/history-file.csv" | |
# Function to export command history to file | |
Function Export-CommandHistory { | |
Get-History | Select-Object -Property CommandLine | Sort-Object -Unique | | |
Export-Csv -Path $historyFile -NoTypeInformation | |
} | |
# Function to import command history from file | |
Function Import-CommandHistory { | |
if (Test-Path $historyFile) { | |
$history = Import-Csv -Path $historyFile -Delimiter "`t" | Select-Object -ExpandProperty CommandLine | |
$uniqueHistory = $history | Select-Object -Unique | |
$uniqueHistory | ForEach-Object { Add-History -InputObject $_ } | |
} | |
} | |
#Trim History file. | |
Function Trim-History { | |
if (Test-Path $historyFile) { | |
$uniqueHistory = Get-Content $historyFile | Select-Object -Unique | |
$uniqueHistory | Set-Content $historyFile | |
Write-Output "Command history file trimmed." | |
} else { | |
Write-Output "Command history file not found." | |
} | |
} | |
# Export command history when exiting PowerShell | |
# $null = Register-EngineEvent -SourceIdentifier PowerShell.Exiting -Action { | |
# Export-CommandHistory | |
# } | |
# Import command history when launching PowerShell | |
#Import-CommandHistory | |
#endregion | |
#region Another attempt to try to give this poor guy some persistant history capabilities | |
# Persistent History | |
# Save last 200 history items on exit | |
$MaximumHistoryCount = 200 | |
$historyPath = Join-Path (split-path $profile) history.clixml | |
# Hook PowerShell's exiting event & hide the registration with -supportEvent | |
Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action { | |
Get-History -Count $MaximumHistoryCount | Export-Clixml $historyPath | |
} | |
# Load previous history, if it exists | |
if ((Test-Path $historyPath)) { | |
Import-Clixml $historyPath | Add-History | |
} | |
#endregion | |
#region Some common posix compliant shims | |
# Emulate the posix's which command | |
function which($cmd) { (Get-Command $cmd).Definition } | |
# Emulate whoami command | |
#function whoami { (get-content env:\userdomain) + "\" + (get-content env:\username) } | |
# Check if the wget2 command is available, Then set an alias to wget. | |
if (Get-Command -Name wget2 -ErrorAction SilentlyContinue) { | |
# If wget2 command is available, set the alias | |
Set-Alias -Name wget -Value wget2 | |
} | |
#endregion | |
# Define a function | |
Function Get-HelloWorld { | |
Write-Output "Hello, World!" | |
} | |
#region Setting up softwares | |
# Common installation paths for Notepad++ | |
$notepadPlusPlusPaths = @("C:\Program Files (x86)\Notepad++\notepad++.exe", "C:\Program Files\Notepad++\notepad++.exe") | |
# Check each path to see if Notepad++ is installed | |
foreach ($path in $notepadPlusPlusPaths) { | |
if (Test-Path $path) { | |
function Open-WithNPP ($filePath) { | |
& $path $filePath | |
} | |
#set a simpler alias for above function because windows is crap. | |
Set-Alias -Name npp -Value Open-WithNPP | |
break | |
} | |
} | |
# setup zoxide if it exists | |
if ((Get-Command zoxide -ErrorAction SilentlyContinue)) { | |
Invoke-Expression (& { (zoxide init powershell | Out-String) }) | |
} | |
#endregion | |
# Define a variable | |
#$MyVariable = "This is my variable." | |
# Clear up screen | |
#clear |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment