-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMicrosoft.PowerShell_profile.ps1
285 lines (260 loc) · 10.3 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
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
<######################################## 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 touch($file) {
"" | Out-File $file -Encoding ASCII
}
function pkill($name) {
Get-Process $name -ErrorAction SilentlyContinue | Stop-Process
}
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() {
Get-Uptime -Since
}
function sudo() {
if ($isAdmin) {
return Write-Host "This instance of PowerShell is already on admin access."
}
if ($args.Count -gt 0) {
if (Test-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 {
if (Test-Path("${env:LOCALAPPDATA}\Microsoft\WindowsApps\wt.exe")) { # windows terminal
Start-Process "${env:LOCALAPPDATA}\Microsoft\WindowsApps\wt.exe" -Verb RunAs
}
elseif (Test-Path("${env:PROGRAMFILES}\PowerShell\7\pwsh.exe")) { # powershell 7
Start-Process -FilePath "${PSHOME}\pwsh.exe" -Verb RunAs
}
else { # powershell v1
Start-Process -FilePath "${env:SystemRoot}\System32\WindowsPowerShell\v1.0\powershell.exe" -Verb RunAs
}
}
}
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 ipaddr() {
(ipconfig | Select-String -Pattern "IPv4 Address. . . . . . . . . . . : (.{1,50}\d)").Matches.Groups[1].Value
}
function pythonpipupgrade() {
pip list --format freeze | ForEach-Object {
pip install --upgrade $_.split('==')[0]
}
}
######################## From My Linux Machine
function codefolder() {
if (Test-Path -Path "F:\Code\code-desktop") {
Set-Location "F:\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 "F:\Kuliah\Mata Kuliah") {
Set-Location "F:\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 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
}
######################## 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() {
param(
[Parameter(ParameterSetName = 'Option')] [ValidateSet("yes", "assume-yes", "assumeyes", "answersyes", "answers-yes", "half-yes","normal", "regular", "update", "upgrade", "cleanup", "deletetempfiles", "deletetemp")] [String] $Option,
[Parameter(ParameterSetName = 'GetHelp')] [ValidateSet("all", "full")] [String] $Help
)
if ($Option -eq "GUI") {
if (Test-Path -Path "F:\Code\WINDOWS\GUI") {
return & "F:\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"
}
}
if ($Help) {
& "${env:USERPROFILE}\Documents\PowerShell\Scripts\Windows-Scripting\SystemUpgrade.ps1" -Help $Help
} else {
& "${env:USERPROFILE}\Documents\PowerShell\Scripts\Windows-Scripting\SystemUpgrade.ps1" -Option $Option
}
}
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-WebRequest -useb "https://christitus.com/win" | Invoke-Expression
}
function XAMPP() {
& "${env:HOMEDRIVE}\xampp\xampp_shell.bat"
}
######### 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 ChocolateyApps() {
Set-Location "${env:HOMEDRIVE}\ProgramData\chocolatey\lib"
}
function AutoHotKeyFolder() {
Set-Location "${env:USERPROFILE}\Documents\AutoHotkey"
}
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
}
function FirefoxProfile() {
Set-Location "${env:APPDATA}\Mozilla\Firefox\Profiles\jrecet5l.default-release"
}
######################## 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
oh-my-posh init pwsh --config $env:POSH_THEMES_PATH/takuya.omp.json | Invoke-Expression
Import-Module -Name Terminal-Icons
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
Import-Module -Name Microsoft.WinGet.CommandNotFound
#f45873b3-b655-43a6-b217-97c00aa0db58
$env:VIRTUAL_ENV_DISABLE_PROMPT = 1 # Disable python venv prompt