forked from alanlivio/ps-sh-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
os_win.ps1
453 lines (387 loc) · 16.9 KB
/
os_win.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# -- essentials --
function log_msg() { Write-Host -ForegroundColor DarkYellow "--" ($args -join " ") }
function log_error() { Write-Host -ForegroundColor DarkRed "--" ($args -join " ") }
function has_sudo() { if (Get-Command sudo -errorAction SilentlyContinue) { return $true } else { return $false } }
function win_enable_sudo() {
if (-Not(Get-Command sudo -errorAction SilentlyContinue)) {
# win 11 support native sudo https://learn.microsoft.com/en-us/windows/sudo/
if ((Get-ComputerInfo | Select-Object -expand OsName) -match 11) {
sudo config --enable
}
# win 10 support from https://github.com/gerardog/gsudo
else {
winget install gsudo
}
win_path_refresh
}
}
function win_update() {
log_msg "win_update"
if (-Not (has_sudo)) { log_error "no sudo. skipping."; return }
log_msg "> winget upgrade"
winget upgrade --accept-package-agreements --accept-source-agreements --silent --all
log_msg "> os upgrade"
if (-Not (has_sudo)) {
log_error "no sudo for os upgrade. starting settings manually"
explorer.exe ms-settings:windowsupdate-action
}
sudo {
# https://gist.github.com/billpieper/a39173afa0b343a14ddeeb1d79ab14ea
if (-Not(Get-Command Install-WindowsUpdate -errorAction SilentlyContinue)) {
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Install-Module -Name PSWindowsUpdate -Scope CurrentUser -Force
# Add-WUServiceManager -MicrosoftUpdate -Confirm:$false | Out-Null
}
$(Install-WindowsUpdate -AcceptAll -IgnoreReboot) | Where-Object {
if ($_ -is [string]) {
$_.Split('', [System.StringSplitOptions]::RemoveEmptyEntries)
}
}
}
}
function win_install_ubuntu() {
if (-Not (has_sudo)) { log_error "no sudo. skipping."; return }
wsl --set-default-version 2
sudo wsl --update
sudo wsl --install -d Ubuntu
}
function winget_install() {
winget list -q $Args[0] | Out-Null
if (-not $?) {
winget install $Args[0] --accept-package-agreements --accept-source-agreements --scope "user"
}
}
function winget_install_at_location() {
winget list -q $Args[0] | Out-Null
if (-not $?) {
winget install $Args[0] --accept-package-agreements --accept-source-agreements --scope "user" --location="$Args[1]"
}
}
function winget_uninstall() {
winget list -q $Args | Out-Null
if ($?) {
winget uninstall --silent "$Args"
}
}
function ps_profile_reload() {
@(
$profile.AllUsersAllHosts,
$profile.AllUsersCurrentHost,
$profile.CurrentUserAllHosts,
$profile.CurrentUserCurrentHost
) | ForEach-Object {
if (Test-Path $_) {
Write-Output "loading $_"
Import-Module -Force -Global $_ #-Verbose
}
}
}
function ps_show_function($name) {
Get-Content Function:\$name
}
function win_hlink_create($path, $target) {
New-Item -ItemType Hardlink -Force -Path $path -Target $target
}
# -- path --
function win_path_add($addPath) {
if (Test-Path $addPath) {
$path = [Environment]::GetEnvironmentVariable('path', 'Machine')
$regexAddPath = [regex]::Escape($addPath)
$arrPath = $path -split ';' | Where-Object { $_ -notMatch "^$regexAddPath\\?" }
$newpath = ($arrPath + $addPath) -join ';'
[Environment]::SetEnvironmentVariable("path", $newpath, 'Machine')
}
else {
Throw "$addPath' is not a valid path."
}
}
function win_path_list() {
(Get-ChildItem Env:Path).Value -split ';'
}
function win_path_refresh() {
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
}
# -- env --
function win_env_add($name, $value) {
[Environment]::SetEnvironmentVariable($name, $value, 'User')
}
function win_env_add_machine($name, $value) {
if (-Not (has_sudo)) { log_error "no sudo. skipping."; return }
sudo {
[Environment]::SetEnvironmentVariable($name, $value, 'Machine')
}
}
function win_env_list() {
[Environment]::GetEnvironmentVariables()
}
# -- explorer --
function win_onedrive_reset() {
& "C:\Program Files\Microsoft OneDrive\onedrive.exe" /reset
}
function win_explorer_hide_home_dotfiles() {
Get-ChildItem "${env:userprofile}\.*" | ForEach-Object { $_.Attributes += "Hidden" }
}
function win_explorer_open_trash() {
Start-Process explorer shell:recyclebinfolder
}
function win_explorer_restart() {
taskkill /f /im explorer.exe | Out-Null
Start-Process explorer.exe
}
# -- wsl --
function wsl_list() {
wsl -l -v
}
function wsl_list_running() {
wsl -l -v --running
}
function wsl_get_default() {
[System.Text.Encoding]::Unicode.GetString([System.Text.Encoding]::UTF8.GetBytes((wsl -l))) -split '\s\s+' | ForEach-Object {
if ($_.Contains('(')) {
return $_.Split(' ')[0]
}
}
}
function wsl_get_default_version() {
Foreach ($i in (wsl -l -v)) {
if ($i.Contains('*')) {
return $i.Split(' ')[-1]
}
}
}
function wsl_terminate() {
wsl -t (wsl_get_default)
}
# -- system --
function win_image_cleanup() {
if (-Not (has_sudo)) { log_error "no sudo. skipping."; return }
sudo { dism /Online /Cleanup-Image /RestoreHealth }
}
function win_policy_reset() {
if (-Not (has_sudo)) { log_error "no sudo. skipping."; return }
sudo {
cmd.exe /C 'RD /S /Q %WinDir%\System32\GroupPolicyUsers '
cmd.exe /C 'RD /S /Q %WinDir%\System32\GroupPolicy '
gpupdate.exe /force
}
}
function win_enable_insider_beta() {
# https://www.elevenforum.com/t/change-windows-insider-program-channel-in-windows-11.795/
bcdedit /set flightsigning on
Set-ItemProperty -Path "HKLM:\Software\Microsoft\WindowsSelfHost\Applicability" -Name "BranchName" -Value 'Beta'
Set-ItemProperty -Path "HKLM:\Software\Microsoft\WindowsSelfHost\Applicability" -Name "ContentType" -Value 'Mainline'
Set-ItemProperty -Path "HKLM:\Software\Microsoft\WindowsSelfHost\Applicability" -Name "Ring" -Value 'External'
Set-ItemProperty -Path "HKLM:\Software\Microsoft\WindowsSelfHost\UI\Selection" -Name "UIBranch" -Value 'Beta'
Set-ItemProperty -Path "HKLM:\Software\Microsoft\WindowsSelfHost\UI\Selection" -Name "UIContentType" -Value 'Mainline'
Set-ItemProperty -Path "HKLM:\Software\Microsoft\WindowsSelfHost\UI\Selection" -Name "UIRing" -Value 'External'
}
function win_enable_dark_no_transparency() {
$reg_personalize = "HKCU:Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"
Set-ItemProperty -Path $reg_personalize -Name "SystemUsesLightTheme" -Value '0' -Type 'DWORD'
Set-ItemProperty -Path $reg_personalize -Name "EnableTransparency" -Value '0' -Type 'DWORD'
Set-ItemProperty -Path $reg_personalize -Name "SystemUsesLightTheme" -Value '0' -Type 'DWORD'
Set-ItemProperty -Path $reg_personalize -Name "ColorPrevalence" -Value '0' -Type 'DWORD'
}
function win_appx_list_installed() {
Get-AppxPackage -User $env:username | ForEach-Object { Write-Output $_.Name }
}
function win_appx_install() {
$pkgs_to_install = ""
foreach ($name in $args) {
if (-Not (Get-AppxPackage -User $env:username -Name $name)) {
$pkgs_to_install = "$pkgs_to_install $name"
}
}
if ($pkgs_to_install) {
log_msg "pkgs_to_install=$pkgs_to_install"
foreach ($pkg in $pkgs_to_install) {
Get-AppxPackage -User $env:username -Name $pkg | ForEach-Object { Add-AppxPackage -ea 0 -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml" } | Out-null
}
}
}
function win_appx_uninstall() {
foreach ($name in $args) {
if (Get-AppxPackage -User $env:username -Name $name) {
log_msg "uninstall $name"
Get-AppxPackage -User $env:username -Name $name | Remove-AppxPackage
}
}
}
# -- system disable --
function win_disable_osapps_unused() {
log_msg "win_disable_osapps_unused"
# old appx not avaliable in winget, most for win10
$pkgs = @(
'Clipchamp.Clipchamp'
'Microsoft.BingNews'
'Microsoft.BingSports'
'Microsoft.BingWeather'
'Microsoft.Getstarted'
'Microsoft.Microsoft3DViewer'
'Microsoft.MicrosoftSolitaireCollection'
'Microsoft.MicrosoftStickyNotes'
'Microsoft.MixedReality.Portal'
'Microsoft.People'
'Microsoft.Wallet'
'microsoft.windowscommunicationsapps'
'Microsoft.WindowsMaps'
'Microsoft.YourPhone'
'Microsoft.ZuneMusic'
'Microsoft.ZuneVideo'
'SpotifyAB.SpotifyMusic'
)
win_appx_uninstall @pkgs
# avaliable in winget
winget_uninstall Microsoft.BingWallpaper
winget_uninstall Microsoft.ZuneVideo_8wekyb3d8bbwe
winget_uninstall Microsoft.MSPaint_8wekyb3d8bbwe
winget_uninstall Microsoft.Skype
winget_uninstall Microsoft.PowerAutomateDesktop
}
function win_disable_password_policy() {
log_msg "win_disable_password_policy"
if (-Not (has_sudo)) { log_error "no sudo. skipping."; return }
sudo {
$tmpfile = New-TemporaryFile
secedit /export /cfg $tmpfile /quiet # this call requires admin
(Get-Content $tmpfile).Replace("PasswordComplexity = 1", "PasswordComplexity = 0").Replace("MaximumPasswordAge = 42", "MaximumPasswordAge = -1") | Out-File $tmpfile
secedit /configure /db "$env:SYSTEMROOT\security\database\local.sdb" /cfg $tmpfile /areas SECURITYPOLICY | Out-Null
Remove-Item -Path $tmpfile
}
}
function win_disable_shortcuts_unused() {
log_msg "win_disable_shortcuts_unused"
# "disable AutoRotation shorcuts"
Set-ItemProperty -Path "HKCU:\Software\Intel\Display\Igfxcui" -Name "HotKeys" -Value 'Enable'
# "disable language shorcuts"
$reg_key_toggle = "HKCU:\Keyboard Layout\Toggle"
Set-ItemProperty -Path $reg_key_toggle -Name "HotKey" -Value '3' -Type 'DWORD'
Set-ItemProperty -Path $reg_key_toggle -Name "Language Hotkey" -Value '3' -Type 'DWORD'
Set-ItemProperty -Path $reg_key_toggle -Name "Layout Hotkey" -Value '3' -Type 'DWORD'
# "disable acessibility shorcuts"
$reg_acess = "HKCU:\Control Panel\Accessibility"
Set-ItemProperty -Path "$reg_acess\ToggleKeys" -Name "Flags" -Value '58' -Type 'DWORD'
New-Item -Path "$reg_acess\Keyboard Response" -Force | Out-Null
Set-ItemProperty -Path "$reg_acess\Keyboard Response" -Name "Flags" -Value '122' -Type 'DWORD'
# explorer restart
Stop-Process -ProcessName explorer -ea 0 | Out-Null
}
function win_disable_sounds() {
log_msg "win_disable_sounds"
Set-ItemProperty -Path "HKCU:\AppEvents\Schemes\" "(Default)" -Value ".None"
Get-ChildItem -Path 'HKCU:\AppEvents\Schemes\Apps' | Get-ChildItem | Get-ChildItem | Where-Object { $_.PSChildName -eq '.Current' } | Set-ItemProperty -Name '(Default)' -Value ''
}
function win_disable_web_search_and_widgets() {
log_msg "win_disable_web_search_and_widgets"
# win 11
if ((Get-ComputerInfo | Select-Object -expand OsName) -match 11) {
winget list -q "MicrosoftWindows.Client.WebExperience_cw5n1h2txyew" | Out-Null
if ($?) { winget.exe uninstall MicrosoftWindows.Client.WebExperience_cw5n1h2txyewy }
# https://www.tomshardware.com/how-to/disable-windows-web-search
if (-Not (has_sudo)) { log_error "no sudo. skipping DisableSearchBoxSuggestions at win 11 ."; return }
sudo {
$reg_explorer_pols = "HKCU:\Software\Policies\Microsoft\Windows\Explorer"
New-Item -Path $reg_explorer_pols -Force | Out-Null
Set-ItemProperty -Path $reg_explorer_pols -Name 'DisableSearchBoxSuggestions' -Value '1' -Type 'DWORD'
}
}
else {
# win 10
# https://www.bennetrichter.de/en/tutorials/windows-10-disable-web-search/
$reg_search = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Search"
Set-ItemProperty -Path "$reg_search" -Name 'BingSearchEnabled' -Value '0' -Type 'DWORD'
$reg_search2 = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\SearchSettings'
Set-ItemProperty -Path "$reg_search2" -Name 'IsDynamicSearchBoxEnabled' -Value '0' -Type 'DWORD'
}
}
function win_disable_edge_ctrl_shift_c() {
log_msg "win_disable_edge_ctrl_shift_c"
if (-Not (has_sudo)) { log_error "no sudo. skipping."; return }
sudo {
$reg_edge_pol = "HKCU:\Software\Policies\Microsoft\Edge"
New-Item -Path $reg_edge_pol -Force | Out-Null
if (-not (Get-ItemPropertyValue -Path $reg_edge_pol -Name 'ConfigureKeyboardShortcuts')) {
Set-ItemProperty -Path $reg_edge_pol -Name 'ConfigureKeyboardShortcuts' -Value '{"disabled": ["dev_tools_elements"]}'
gpupdate.exe /force
}
}
}
function win_disable_explorer_clutter() {
log_msg "win_disable_explorer_clutter"
$reg_explorer = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer"
# setup folder listing
Set-ItemProperty -Path $reg_explorer -Name ShowFrequent -Value '0' -Type 'DWORD'
Set-ItemProperty -Path $reg_explorer -Name ShowRecent -Value '0' -Type 'DWORD'
Set-ItemProperty -Path $reg_explorer -Name ShowRecommendations -Value '0' -Type 'DWORD'
Set-ItemProperty -Path $reg_explorer -Name HideFileExt -Value '0' -Type 'DWORD'
}
function win_disable_taskbar_clutter() {
log_msg "win_disable_taskbar_clutter"
$reg_explorer_adv = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"
# taskbar
# https://www.askvg.com/disable-or-remove-extra-icons-and-buttons-from-windows-11-taskbar
Set-ItemProperty -Path $reg_explorer_adv -Name ShowTaskViewButton -Value '0' -Type 'DWORD'
Set-ItemProperty -Path $reg_explorer_adv -Name TaskbarDa -Value '0' -Type 'DWORD'
Set-ItemProperty -Path $reg_explorer_adv -Name TaskbarMn -Value '0' -Type 'DWORD'
Set-ItemProperty -Path $reg_explorer_adv -Name ShowCopilotButton -Value '0' -Type 'DWORD'
Set-ItemProperty -Path $reg_explorer_adv -Name UseCompactMode -Value '1' -Type 'DWORD'
Set-ItemProperty -Path $reg_explorer_adv -Name ShowStatusBar -Value '1' -Type 'DWORD'
Set-ItemProperty -Path $reg_explorer_adv -Name TaskbarAI -Value '0' -Type 'DWORD'
Set-ItemProperty -Path $reg_explorer_adv -Name TaskbarBadges -Value '0' -Type 'DWORD'
Set-ItemProperty -Path $reg_explorer_adv -Name TaskbarAnimations -Value '0' -Type 'DWORD'
# multitasking
# https://www.itechtics.com/disable-edge-tabs-alt-tab
Set-ItemProperty -Path $reg_explorer_adv -Name MultiTaskingAltTabFilter -Value '3' -Type 'DWORD'
# https://superuser.com/questions/1516878/how-to-disable-windows-snap-assist-via-command-line
Set-ItemProperty -Path $reg_explorer_adv -Name SnapAssist -Value '0' -Type 'DWORD'
# search
$reg_search = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Search"
Set-ItemProperty -Path $reg_search -Name SearchBoxTaskbarMode -Value '0' -Type 'DWORD'
}
function win_disable_copilot() {
sudo {
$reg_explorer_pols = "HKCU:\Software\Policies\Microsoft\Windows"
New-Item -Path "$reg_explorer_pols\WindowsCopilot" -Force | Out-Null
Set-ItemProperty -Path "$reg_explorer_pols\WindowsCopilot" -Name 'TurnOffWindowsCopilot' -Value '1' -Type 'DWORD'
}
}
function win_disable_gaming_clutter() {
log_msg "win_disable_gaming_clutter"
# https://www.makeuseof.com/windows-new-app-ms-gamingoverlay-error/
$reg_game_dvr = "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\GameDVR"
Set-ItemProperty -Path $reg_game_dvr -Name AppCaptureEnabled -Value '0' -Type 'DWORD'
Set-ItemProperty -Path $reg_game_dvr -Name HistoricalCaptureEnabled -Value '0' -Type 'DWORD'
$reg_game_store = "HKCU:\System\GameConfigStore"
Set-ItemProperty -Path $reg_game_store -Name GameDVR_Enabled -Value '0' -Type 'DWORD'
winget_uninstall Microsoft.Xbox.TCUI_8wekyb3d8bbwe
winget_uninstall Microsoft.XboxApp_8wekyb3d8bbwe
winget_uninstall Microsoft.XboxGameOverlay_8wekyb3d8bbwe
winget_uninstall Microsoft.XboxGamingOverlay_8wekyb3d8bbwe
winget_uninstall Microsoft.XboxIdentityProvider_8wekyb3d8bbwe
winget_uninstall Microsoft.XboxSpeechToTextOverlay_8wekyb3d8bbwe
}
# -- system enable --
function win_enable_osapps_essentials() {
log_msg "win_enable_osapps_essentials"
$pkgs = @(
'Microsoft.WindowsStore'
'Microsoft.WindowsCalculator'
'Microsoft.Windows.Photos'
'Microsoft.WindowsFeedbackHub'
'Microsoft.WindowsCamera'
)
win_appx_install @pkgs
}
function win_enable_hyperv() {
if (-Not (has_sudo)) { log_error "no sudo. skipping."; return }
sudo { dism /online /enable-feature /featurename:Microsoft-Hyper-V -All /LimitAccess /ALL }
}
function win_ssh_agent_and_add_id_rsa() {
if (-Not (has_sudo)) { log_error "no sudo. skipping."; return }
sudo {
Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent
Get-Service ssh-agent
}
ssh-add "$env:userprofile\\.ssh\\id_rsa"
}