-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMicrosoft.PowerShell_profile.ps1
More file actions
387 lines (356 loc) · 15 KB
/
Microsoft.PowerShell_profile.ps1
File metadata and controls
387 lines (356 loc) · 15 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<######################################## PowerShell Template From : ###############################
- https://gist.github.com/timsneath/19867b12eee7fd5af2ba
- https://github.com/ChrisTitusTech/powershell-profile/blob/main/Microsoft.PowerShell_profile.ps1
####################################################################################################>
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal $identity
$isAdmin = $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
#######################################################################################################################
# Custom Aliases #
#######################################################################################################################
######################## Linux Style Aliases
function whereis() {
(Get-Command $args).Source
}
function touch($file) {
"" | Out-File $file -Encoding ASCII
}
function pkill($name) {
taskkill /IM "${name}.exe" /F 2>$null
}
function find($name) {
Get-ChildItem -Recurse -Filter "*${name}*" -ErrorAction SilentlyContinue | ForEach-Object {
$place_path = $_.directory
Write-Output "${place_path}\${_}"
}
}
function unzip($path, $destination) {
Write-Host "Extracting ${path} to ${destination}"
if (!$destination) {
return Expand-Archive -Path "${path}" -DestinationPath "${pwd}"
}
Expand-Archive -Path "${path}" -DestinationPath "${destination}"
}
function df() {
if ($args -eq "-H") { Get-Volume }
}
function export() {
$env:PATH -Replace ";", "`n"
}
function uptime() {
try {
# find date/time format
$dateFormat = [System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat.ShortDatePattern
$timeFormat = [System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat.LongTimePattern
# check powershell version
if ($PSVersionTable.PSVersion.Major -eq 5) {
$lastBoot = (Get-WmiObject win32_operatingsystem).LastBootUpTime
$bootTime = [System.Management.ManagementDateTimeConverter]::ToDateTime($lastBoot)
# reformat lastBoot
$lastBoot = $bootTime.ToString("$dateFormat $timeFormat")
} else {
# the Get-Uptime cmdlet was introduced in PowerShell 6.0
$lastBoot = (Get-Uptime -Since).ToString("$dateFormat $timeFormat")
$bootTime = [System.DateTime]::ParseExact($lastBoot, "$dateFormat $timeFormat", [System.Globalization.CultureInfo]::InvariantCulture)
}
# Format the start time
$formattedBootTime = $bootTime.ToString("dddd, MMMM dd, yyyy HH:mm:ss", [System.Globalization.CultureInfo]::InvariantCulture) + " [$lastBoot]"
Write-Host "System started on: $formattedBootTime" -ForegroundColor DarkGray
# calculate uptime
$uptime = (Get-Date) - $bootTime
# Uptime in days, hours, minutes, and seconds
$days = $uptime.Days
$hours = $uptime.Hours
$minutes = $uptime.Minutes
$seconds = $uptime.Seconds
# Uptime output
Write-Host ("Uptime: {0} days, {1} hours, {2} minutes, {3} seconds" -f $days, $hours, $minutes, $seconds) -ForegroundColor Blue
} catch {
Write-Error "An error occurred while retrieving system uptime."
}
}
function sudo() {
if ($isAdmin) {
return Write-Host "This instance of PowerShell is already on admin access."
}
if ($args.Count -gt 0) {
if (Test-Path -Path "${env:PROGRAMFILES}\PowerShell\7\pwsh.exe") { # powershell 7
Start-Process -FilePath "${PSHOME}\pwsh.exe" -Verb RunAs -ArgumentList "-NoExit", "-Command ${args}"
}
else { # powershell v1
Start-Process -FilePath "${env:SystemRoot}\System32\WindowsPowerShell\v1.0\powershell.exe" -Verb RunAs -ArgumentList "-NoExit", "-Command ${args}"
}
}
else {
# no arguments provided
if (Test-Path -Path "${env:LOCALAPPDATA}\Microsoft\WindowsApps\wt.exe") { # windows terminal
Start-Process "${env:LOCALAPPDATA}\Microsoft\WindowsApps\wt.exe" -Verb RunAs -ArgumentList "-d `"${pwd}`""
}
elseif (Test-Path -Path "${env:PROGRAMFILES}\PowerShell\7\pwsh.exe") { # powershell 7
Start-Process -FilePath "${PSHOME}\pwsh.exe" -Verb RunAs -WorkingDirectory "${pwd}"
}
else { # powershell v1
Start-Process -FilePath "${env:SystemRoot}\System32\WindowsPowerShell\v1.0\powershell.exe" -Verb RunAs -WorkingDirectory "${pwd}"
}
exit
}
}
Set-Alias -Name ll -Value Get-ChildItem
Set-Alias -Name l -Value Get-ChildItem
Set-Alias -Name read -Value Read-Host
function file() {
if ($args.Count -gt 0) {
foreach ($path in $args) {
Start-Process "${path}"
}
}
else {
Start-Process $args
}
}
function open() {
if ($args.Count -gt 0) {
foreach ($app in $args) {
& "${app}"
}
}
else {
& $args
}
}
function reboot() {
shutdown /r /t 0 /c "Restarting system"
}
function poweroff() {
shutdown /s /t 0 /c "Shutdown system"
}
function ip() {
if ($args -eq "addr" -or $args -eq "a" -or $args -eq "address") {
(ipconfig | Select-String -Pattern "IPv4 Address. . . . . . . . . . . : (.{1,50}\d)").Matches.Groups[1].Value
}
}
function pubip() {
(Invoke-WebRequest http://ifconfig.me/ip).Content
}
function pythonpipupgrade() {
pip list --format freeze | ForEach-Object {
pip install --upgrade $_.split('==')[0]
}
}
function timer($amount, $unit) {
if ($unit -eq "sec") {
for ($i = $amount; $i -ge 0; $i--) {
Clear-Host
Write-Host $i
Start-Sleep 1
}
} elseif ($unit -eq "min") {
for ($i = ($amount * 60); $i -ge 0; $i--) {
Clear-Host
Write-Host $i
Start-Sleep 1
}
}
ShowNotification "Done!" "Timer Finished!"
}
function bash() {
& "${env:ProgramFiles}\Git\bin\bash.exe"
}
function convert() {
if (Test-Path -Path "E:\UDIN\Code\code-desktop\Python\Python-Currency-Converter\convert_currency.py") {
python E:\UDIN\Code\code-desktop\Python\Python-Currency-Converter\convert_currency.py @args
} elseif (Test-Path -Path "${env:USERPROFILE}\Documents\Code Folder\code-desktop\Python\Python-Currency-Converter\convert_currency.py") {
python "${env:USERPROFILE}\Documents\Code Folder\code-desktop\Python\Python-Currency-Converter\convert_currency.py" @args
}
}
function temperature() {
if (Test-Path -Path "E:\UDIN\Code\code-desktop\Python\Python-Temperature-Converter\convert_temperature.py") {
python E:\UDIN\Code\code-desktop\Python\Python-Temperature-Converter\convert_temperature.py @args
} elseif (Test-Path -Path "${env:USERPROFILE}\Documents\Code Folder\code-desktop\Python\Python-Temperature-Converter\convert_temperature.py") {
python "${env:USERPROFILE}\Documents\Code Folder\code-desktop\Python\Python-Temperature-Converter\convert_temperature.py" @args
}
}
function speedtest() {
if (Test-Path -Path "E:\UDIN\Code\code-desktop\Python\Python-Automation-Scripts\test_connection.py") {
python E:\UDIN\Code\code-desktop\Python\Python-Automation-Scripts\test_connection.py @args
} elseif (Test-Path -Path "${env:USERPROFILE}\Documents\Code Folder\code-desktop\Python\Python-Automation-Scripts\test_connection.py") {
python "${env:USERPROFILE}\Documents\Code Folder\code-desktop\Python\Python-Automation-Scripts\test_connection.py" @args
}
}
######################## From My Linux Machine
function codefolder() {
if (Test-Path -Path "E:\UDIN\Code\code-desktop") {
Set-Location "E:\UDIN\Code\code-desktop"
} elseif (Test-Path -Path "${env:USERPROFILE}\Documents\Code Folder\code-desktop") {
Set-Location "${env:USERPROFILE}\Documents\Code Folder\code-desktop"
}
}
function kuliah() {
if (Test-Path -Path "E:\UDIN\Kuliah\Mata Kuliah") {
Set-Location "E:\UDIN\Kuliah\Mata Kuliah"
} elseif (Test-Path -Path "${env:USERPROFILE}\Documents\Kuliah\Mata Kuliah") {
Set-Location "${env:USERPROFILE}\Documents\Kuliah\Mata Kuliah"
}
}
function scrcpyupdate() {
& "${env:USERPROFILE}\Documents\PowerShell\Scripts\Windows-Scripting\ScreenCopyUpdate.ps1"
}
function ytdlp() {
& "${env:USERPROFILE}\Documents\PowerShell\Scripts\Windows-Scripting\ytdlpscript.ps1" @args
}
function matrix() {
& "${env:USERPROFILE}\Documents\PowerShell\Scripts\Windows-Scripting\Matrix.bat"
}
function phone() {
& "${env:HOMEDRIVE}\scrcpy\scrcpy.exe" --video-bit-rate=20M --turn-screen-off --stay-awake
}
function sound() {
mmsys.cpl sounds,1
}
function mirror() {
sound
phone
}
function editrc() {
code $PROFILE
}
function reload() {
. $PROFILE
}
function sync() {
syncthing --no-browser
}
function hosts() {
& $env:SystemRoot\System32\drivers\etc\hosts
}
######################## Windows Style Aliases
function md5() { Get-FileHash -Algorithm MD5 $args }
function sha1() { Get-FileHash -Algorithm SHA1 $args }
function sha256() { Get-FileHash -Algorithm SHA256 $args }
function ShowNotification($title, $text) {
# windows 10 notification balloon
Add-Type -AssemblyName System.Windows.Forms
$global:BalloonNotification = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$BalloonNotification.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$BalloonNotification.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info
$BalloonNotification.BalloonTipText = "${text}"
$BalloonNotification.BalloonTipTitle = "${title}"
$BalloonNotification.Visible = $true
$BalloonNotification.ShowBalloonTip(5000)
}
######### Run Scripts #########
function SystemUpgrade() {
# SystemUpgrade -GUI
if ($args.Count -gt 0 -and $args.ToLower() -eq "-gui") {
if (Test-Path -Path "E:\UDIN\Code\WINDOWS\GUI") {
return & "E:\UDIN\Code\WINDOWS\GUI\SystemUpgrade-GUI.ps1"
} elseif (Test-Path -Path "${env:USERPROFILE}\Documents\Code Folder\Windows\GUI") {
return & "${env:USERPROFILE}\Documents\Code Folder\Windows\GUI\SystemUpgrade-GUI.ps1"
} else {
return Write-Host "Path for GUI scripts not found." -ForegroundColor Red
}
}
& "${env:USERPROFILE}\Documents\PowerShell\Scripts\Windows-Scripting\SystemUpgrade.ps1" @args
}
function ChangeOutputDevice() {
& "${env:USERPROFILE}\Documents\PowerShell\Scripts\Windows-Scripting\ChangeOutputDevice.ps1"
}
function NetSpeedMonitor() {
& "${env:USERPROFILE}\Documents\PowerShell\Scripts\Windows-Scripting\NetSpeedMonitor.ps1"
}
function WinUtil() {
Invoke-RestMethod "https://christitus.com/win" | Invoke-Expression
}
function XAMPP() {
& "${env:HOMEDRIVE}\xampp\xampp_shell.bat"
}
function AutoLaunch() {
& "${env:USERPROFILE}\Documents\PowerShell\Scripts\Windows-Scripting\AutoLaunchApp.ps1"
}
######### Change Folder #########
function Scripts() {
Set-Location "${env:USERPROFILE}\Documents\PowerShell\Scripts\Windows-Scripting"
}
function USBScripts() {
Set-Location "F:\Code\WINDOWS\Scripts"
}
function WindowsUpdateFolder() {
Set-Location "${env:windir}\SoftwareDistribution\Download"
}
function WinGetFolder {
Set-Location "${env:LOCALAPPDATA}\Microsoft\WinGet"
}
function ChocolateyApps() {
Set-Location "${env:HOMEDRIVE}\ProgramData\chocolatey\lib"
}
function AutoHotKeyFolder() {
Set-Location "${env:USERPROFILE}\Documents\AutoHotkey"
}
function FirefoxProfile() {
Set-Location "${env:APPDATA}\Mozilla\Firefox\Profiles\jrecet5l.default-release"
}
function SignOut() {
shutdown /L
}
function RestartToUEFI() {
shutdown /r /fw
}
function RestartToRecovery() {
shutdown /r /o
}
function WindowsUpdateChoose($kbarticleid) {
Get-WindowsUpdate -Install -AcceptAll -KBArticleID "${kbarticleid}"
}
function WindowsUpdateAll() {
Get-WindowsUpdate -Install -AcceptAll
}
######################## Application Shortcut (admin)
function firefox() {
Start-Process -FilePath "${env:ProgramFiles}\Mozilla Firefox\firefox.exe" -Verb RunAs
}
function discord() {
Start-Process -FilePath "${env:LOCALAPPDATA}\Discord\Update.exe" -Verb RunAs
}
#######################################################################################################################
# Requirement for Oh-My-Posh, Chocolatey, Winget #
#######################################################################################################################
# Import PowerShell Theme from oh-my-posh
# More Themes : https://ohmypo.sh/docs/themes
oh-my-posh init pwsh --config $env:POSH_THEMES_PATH/takuya.omp.json | Invoke-Expression
if (!(Get-Module -ListAvailable -Name Terminal-Icons -ErrorAction SilentlyContinue)) {
Install-Module -Name Terminal-Icons -Scope CurrentUser -Force -SkipPublisherCheck
}
Import-Module -Name Terminal-Icons
if (!(Get-Module -ListAvailable -Name posh-git -ErrorAction SilentlyContinue)) {
Install-Module -Name posh-git -Scope CurrentUser -Force -SkipPublisherCheck
}
Import-Module posh-git
# Import the Chocolatey Profile that contains the necessary code to enable
# tab-completions to function for `choco`.
# Be aware that if you are missing these lines from your profile, tab completion
# for `choco` will not function.
# See https://ch0.co/tab-completion for details.
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
# Tab-compleation for winget
Register-ArgumentCompleter -Native -CommandName winget -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
[Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.Utf8Encoding]::new()
$Local:word = $wordToComplete.Replace('"', '""')
$Local:ast = $commandAst.ToString().Replace('"', '""')
winget complete --word="$Local:word" --commandline "$Local:ast" --position $cursorPosition | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
Remove-Variable identity
Remove-Variable principal
#f45873b3-b655-43a6-b217-97c00aa0db58 PowerToys CommandNotFound module
if (!(Get-Module -ListAvailable -Name Microsoft.WinGet.CommandNotFound -ErrorAction SilentlyContinue)) {
Install-Module -Name Microsoft.WinGet.CommandNotFound -Scope CurrentUser -Force -SkipPublisherCheck
}
Import-Module -Name Microsoft.WinGet.CommandNotFound
#f45873b3-b655-43a6-b217-97c00aa0db58
$env:VIRTUAL_ENV_DISABLE_PROMPT = 1 # Disable python venv prompt