-
Notifications
You must be signed in to change notification settings - Fork 160
/
Microsoft.PowerShell-profile.ps1
160 lines (127 loc) · 3.82 KB
/
Microsoft.PowerShell-profile.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# 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