Skip to content

Instantly share code, notes, and snippets.

@nicholasadamou
Created February 16, 2019 12:36
Show Gist options
  • Save nicholasadamou/79883d867403d4c2c4be4bf3161cecae to your computer and use it in GitHub Desktop.
Save nicholasadamou/79883d867403d4c2c4be4bf3161cecae to your computer and use it in GitHub Desktop.
PowerShell profile
# Increase history
$MaximumHistoryCount = 10000
# Produce UTF-8 by default
$PSDefaultParameterValues["Out-File:Encoding"]="utf8"
# Show selection menu for tab
Set-PSReadlineKeyHandler -Chord Tab -Function MenuComplete
# Helper Functions
#######################################################
function uptime {
Get-WmiObject win32_operatingsystem | select csname, @{LABEL='LastBootUpTime';
EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
}
function reload-profile {
& $profile
}
function find-file($name) {
ls -recurse -filter "*${name}*" -ErrorAction SilentlyContinue | foreach {
$place_path = $_.directory
echo "${place_path}\${_}"
}
}
function print-path {
($Env:Path).Split(";")
}
function unzip ($file) {
$dirname = (Get-Item $file).Basename
echo("Extracting", $file, "to", $dirname)
New-Item -Force -ItemType directory -Path $dirname
expand-archive $file -OutputPath $dirname -ShowProgress
}
# Unixlike commands
#######################################################
function df {
get-volume
}
function sed($file, $find, $replace){
(Get-Content $file).replace("$find", $replace) | Set-Content $file
}
function sed-recursive($filePattern, $find, $replace) {
$files = ls . "$filePattern" -rec
foreach ($file in $files) {
(Get-Content $file.PSPath) |
Foreach-Object { $_ -replace "$find", "$replace" } |
Set-Content $file.PSPath
}
}
function grep($regex, $dir) {
if ( $dir ) {
ls $dir | select-string $regex
return
}
$input | select-string $regex
}
function grepv($regex) {
$input | ? { !$_.Contains($regex) }
}
function which($name) {
Get-Command $name | Select-Object -ExpandProperty Definition
}
function export($name, $value) {
set-item -force -path "env:$name" -value $value;
}
function pkill($name) {
ps $name -ErrorAction SilentlyContinue | kill
}
function pgrep($name) {
ps $name
}
function touch($file) {
"" | Out-File $file -Encoding ASCII
}
function sudo {
$file, [string]$arguments = $args;
$psi = new-object System.Diagnostics.ProcessStartInfo $file;
$psi.Arguments = $arguments;
$psi.Verb = "runas";
$psi.WorkingDirectory = get-location;
[System.Diagnostics.Process]::Start($psi) >> $null
}
# https://gist.github.com/aroben/5542538
function pstree {
$ProcessesById = @{}
foreach ($Process in (Get-WMIObject -Class Win32_Process)) {
$ProcessesById[$Process.ProcessId] = $Process
}
$ProcessesWithoutParents = @()
$ProcessesByParent = @{}
foreach ($Pair in $ProcessesById.GetEnumerator()) {
$Process = $Pair.Value
if (($Process.ParentProcessId -eq 0) -or !$ProcessesById.ContainsKey($Process.ParentProcessId)) {
$ProcessesWithoutParents += $Process
continue
}
if (!$ProcessesByParent.ContainsKey($Process.ParentProcessId)) {
$ProcessesByParent[$Process.ParentProcessId] = @()
}
$Siblings = $ProcessesByParent[$Process.ParentProcessId]
$Siblings += $Process
$ProcessesByParent[$Process.ParentProcessId] = $Siblings
}
function Show-ProcessTree([UInt32]$ProcessId, $IndentLevel) {
$Process = $ProcessesById[$ProcessId]
$Indent = " " * $IndentLevel
if ($Process.CommandLine) {
$Description = $Process.CommandLine
} else {
$Description = $Process.Caption
}
Write-Output ("{0,6}{1} {2}" -f $Process.ProcessId, $Indent, $Description)
foreach ($Child in ($ProcessesByParent[$ProcessId] | Sort-Object CreationDate)) {
Show-ProcessTree $Child.ProcessId ($IndentLevel + 4)
}
}
Write-Output ("{0,6} {1}" -f "PID", "Command Line")
Write-Output ("{0,6} {1}" -f "---", "------------")
foreach ($Process in ($ProcessesWithoutParents | Sort-Object CreationDate)) {
Show-ProcessTree $Process.ProcessId 0
}
}
# Aliases
#######################################################
function pull () { & get pull $args }
function checkout () { & git checkout $args }
del alias:gc -Force
del alias:gp -Force
Set-Alias -Name gc -Value checkout
Set-Alias -Name gp -Value pull
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment