code format using powershell VS code extension

This commit is contained in:
Rahul Somasundaram 2022-05-14 21:48:29 +05:30
parent eeba67405d
commit f992437f7f
No known key found for this signature in database
GPG Key ID: 466FF35FC278207A

View File

@ -8,33 +8,35 @@
# $inputXML = Get-Content "MainWindow.xaml" #uncomment for development
$inputXML = (new-object Net.WebClient).DownloadString("https://raw.githubusercontent.com/ChrisTitusTech/winutil/main/MainWindow.xaml") #uncomment for Production
$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
$inputXML = $inputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^<Win.*', '<Window'
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
try { $Form = [Windows.Markup.XamlReader]::Load( $reader ) }
catch [System.Management.Automation.MethodInvocationException] {
Write-Warning "We ran into a problem with the XAML code. Check the syntax for this control..."
write-host $error[0].Exception.Message -ForegroundColor Red
if ($error[0].Exception.Message -like "*button*"){
write-warning "Ensure your &lt;button in the `$inputXML does NOT have a Click=ButtonClick property. PS can't handle this`n`n`n`n"}
}
catch{#if it broke some other way <img draggable="false" role="img" class="emoji" alt="😀" src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/svg/1f600.svg">
Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."
if ($error[0].Exception.Message -like "*button*") {
write-warning "Ensure your &lt;button in the `$inputXML does NOT have a Click=ButtonClick property. PS can't handle this`n`n`n`n"
}
}
catch {
#if it broke some other way <img draggable="false" role="img" class="emoji" alt="😀" src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/svg/1f600.svg">
Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."
}
#===========================================================================
# Store Form Objects In PowerShell
#===========================================================================
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}
$xaml.SelectNodes("//*[@Name]") | % { Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) }
Function Get-FormVariables{
if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}
write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
get-variable WPF*
Function Get-FormVariables {
if ($global:ReadmeDisplay -ne $true) { Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow; $global:ReadmeDisplay = $true }
write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
get-variable WPF*
}
Get-FormVariables
@ -48,7 +50,7 @@ $WPFTab1BT.Add_Click({
$WPFTabNav.Items[1].IsSelected = $false
$WPFTabNav.Items[2].IsSelected = $false
$WPFTabNav.Items[3].IsSelected = $false
})
})
$WPFTab2BT.Add_Click({
$WPFTabNav.Items[0].IsSelected = $false
$WPFTabNav.Items[1].IsSelected = $true
@ -146,7 +148,6 @@ $WPFinstall.Add_Click({
$wingetinstall.Add("JetBrains.Toolbox")
$WPFInstalljetbrains.IsChecked = $false
}
If ( $WPFInstallmpc.IsChecked -eq $true ) {
$wingetinstall.Add("clsid2.mpc-hc")
$WPFInstallmpc.IsChecked = $false
@ -349,7 +350,6 @@ $WPFinstall.Add_Click({
$wingetinstall.Add("Microsoft.Teams")
$WPFInstallteams.IsChecked = $false
}
If ( $WPFInstalltreesize.IsChecked -eq $true ) {
$wingetinstall.Add("JAMSoftware.TreeSize.Free")
$WPFInstalltreesize.IsChecked = $false
@ -383,8 +383,7 @@ $WPFinstall.Add_Click({
$wingetinstall.ToArray()
# Define Output variable
$wingetResult = New-Object System.Collections.Generic.List[System.Object]
foreach ( $node in $wingetinstall )
{
foreach ( $node in $wingetinstall ) {
Start-Process powershell.exe -Verb RunAs -ArgumentList "-command winget install -e --accept-source-agreements --accept-package-agreements --silent $node | Out-Host" -Wait -WindowStyle Maximized
$wingetResult.Add("$node`n")
}
@ -397,9 +396,9 @@ $WPFinstall.Add_Click({
$Messageboxbody = ($wingetResult)
$MessageIcon = [System.Windows.MessageBoxImage]::Information
[System.Windows.MessageBox]::Show($Messageboxbody,$MessageboxTitle,$ButtonType,$MessageIcon)
[System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $MessageIcon)
})
})
$WPFInstallUpgrade.Add_Click({
Start-Process powershell.exe -Verb RunAs -ArgumentList "-command winget upgrade --all | Out-Host" -Wait -WindowStyle Maximized
@ -409,8 +408,8 @@ $WPFInstallUpgrade.Add_Click({
$Messageboxbody = ("Done")
$MessageIcon = [System.Windows.MessageBoxImage]::Information
[System.Windows.MessageBox]::Show($Messageboxbody,$MessageboxTitle,$ButtonType,$MessageIcon)
})
[System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $MessageIcon)
})
#===========================================================================
# Tab 2 - Tweaks Buttons
@ -433,7 +432,7 @@ $WPFdesktop.Add_Click({
$WPFMiscTweaksNum.IsChecked = $true
$WPFMiscTweaksLapPower.IsChecked = $false
$WPFMiscTweaksLapNum.IsChecked = $false
})
})
$WPFlaptop.Add_Click({
@ -453,7 +452,7 @@ $WPFlaptop.Add_Click({
$WPFMiscTweaksLapNum.IsChecked = $true
$WPFMiscTweaksPower.IsChecked = $false
$WPFMiscTweaksNum.IsChecked = $false
})
})
$WPFminimal.Add_Click({
@ -473,7 +472,7 @@ $WPFminimal.Add_Click({
$WPFMiscTweaksNum.IsChecked = $false
$WPFMiscTweaksLapPower.IsChecked = $false
$WPFMiscTweaksLapNum.IsChecked = $false
})
})
$WPFtweaksbutton.Add_Click({
@ -717,7 +716,8 @@ $WPFtweaksbutton.Add_Click({
Stop-Process $taskmgr
$preferences.Preferences[28] = 0
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\TaskManager" -Name "Preferences" -Type Binary -Value $preferences.Preferences
} else {Write-Host "Task Manager patch not run in builds 22557+ due to bug"}
}
else { Write-Host "Task Manager patch not run in builds 22557+ due to bug" }
Write-Host "Showing file operations details..."
If (!(Test-Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\OperationStatusManager")) {
@ -836,7 +836,7 @@ $WPFtweaksbutton.Add_Click({
Write-Host "Adjusting visual effects for performance..."
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name "DragFullWindows" -Type String -Value 0
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name "MenuShowDelay" -Type String -Value 200
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name "UserPreferencesMask" -Type Binary -Value ([byte[]](144,18,3,128,16,0,0,0))
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name "UserPreferencesMask" -Type Binary -Value ([byte[]](144, 18, 3, 128, 16, 0, 0, 0))
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop\WindowMetrics" -Name "MinAnimate" -Type String -Value 0
Set-ItemProperty -Path "HKCU:\Control Panel\Keyboard" -Name "KeyboardDelay" -Type DWord -Value 0
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -Name "ListviewAlphaSelect" -Type DWord -Value 0
@ -943,7 +943,7 @@ $WPFtweaksbutton.Add_Click({
Write-Host "Removing Bloatware"
foreach ($Bloat in $Bloatware) {
Get-AppxPackage -Name $Bloat| Remove-AppxPackage
Get-AppxPackage -Name $Bloat | Remove-AppxPackage
Get-AppxProvisionedPackage -Online | Where-Object DisplayName -like $Bloat | Remove-AppxProvisionedPackage -Online
Write-Host "Trying to remove $Bloat."
}
@ -956,8 +956,8 @@ $WPFtweaksbutton.Add_Click({
$Messageboxbody = ("Done")
$MessageIcon = [System.Windows.MessageBoxImage]::Information
[System.Windows.MessageBox]::Show($Messageboxbody,$MessageboxTitle,$ButtonType,$MessageIcon)
})
[System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $MessageIcon)
})
#===========================================================================
# Undo All
#===========================================================================
@ -1074,7 +1074,7 @@ $WPFundoall.Add_Click({
Write-Output "Adjusting visual effects for appearance..."
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name "DragFullWindows" -Type String -Value 1
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name "MenuShowDelay" -Type String -Value 400
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name "UserPreferencesMask" -Type Binary -Value ([byte[]](158,30,7,128,18,0,0,0))
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name "UserPreferencesMask" -Type Binary -Value ([byte[]](158, 30, 7, 128, 18, 0, 0, 0))
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop\WindowMetrics" -Name "MinAnimate" -Type String -Value 1
Set-ItemProperty -Path "HKCU:\Control Panel\Keyboard" -Name "KeyboardDelay" -Type DWord -Value 1
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -Name "ListviewAlphaSelect" -Type DWord -Value 1
@ -1095,8 +1095,8 @@ $WPFundoall.Add_Click({
$Messageboxbody = ("Done")
$MessageIcon = [System.Windows.MessageBoxImage]::Information
[System.Windows.MessageBox]::Show($Messageboxbody,$MessageboxTitle,$ButtonType,$MessageIcon)
})
[System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $MessageIcon)
})
#===========================================================================
# Tab 3 - Config Buttons
#===========================================================================
@ -1145,52 +1145,52 @@ $WPFFeatureInstall.Add_Click({
$Messageboxbody = ("Done")
$MessageIcon = [System.Windows.MessageBoxImage]::Information
[System.Windows.MessageBox]::Show($Messageboxbody,$MessageboxTitle,$ButtonType,$MessageIcon)
})
[System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $MessageIcon)
})
$WPFPanelcontrol.Add_Click({
cmd /c control
})
})
$WPFPanelnetwork.Add_Click({
cmd /c ncpa.cpl
})
cmd /c ncpa.cpl
})
$WPFPanelpower.Add_Click({
cmd /c powercfg.cpl
})
cmd /c powercfg.cpl
})
$WPFPanelsound.Add_Click({
cmd /c mmsys.cpl
})
cmd /c mmsys.cpl
})
$WPFPanelsystem.Add_Click({
cmd /c sysdm.cpl
})
cmd /c sysdm.cpl
})
$WPFPaneluser.Add_Click({
cmd /c "control userpasswords2"
})
cmd /c "control userpasswords2"
})
#===========================================================================
# Tab 4 - Updates Buttons
#===========================================================================
$WPFUpdatesdefault.Add_Click({
# Source: https://github.com/rgl/windows-vagrant/blob/master/disable-windows-updates.ps1 reversed!
# Source: https://github.com/rgl/windows-vagrant/blob/master/disable-windows-updates.ps1 reversed!
Set-StrictMode -Version Latest
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'
trap {
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'
trap {
Write-Host
Write-Host "ERROR: $_"
Write-Host (($_.ScriptStackTrace -split '\r?\n') -replace '^(.*)$','ERROR: $1')
Write-Host (($_.Exception.ToString() -split '\r?\n') -replace '^(.*)$','ERROR EXCEPTION: $1')
Write-Host (($_.ScriptStackTrace -split '\r?\n') -replace '^(.*)$', 'ERROR: $1')
Write-Host (($_.Exception.ToString() -split '\r?\n') -replace '^(.*)$', 'ERROR EXCEPTION: $1')
Write-Host
Write-Host 'Sleeping for 60m to give you time to look around the virtual machine before self-destruction...'
Start-Sleep -Seconds (60*60)
Start-Sleep -Seconds (60 * 60)
Exit 1
}
}
# disable automatic updates.
# XXX this does not seem to work anymore.
# see How to configure automatic updates by using Group Policy or registry settings
# at https://support.microsoft.com/en-us/help/328010
function New-Directory($path) {
# disable automatic updates.
# XXX this does not seem to work anymore.
# see How to configure automatic updates by using Group Policy or registry settings
# at https://support.microsoft.com/en-us/help/328010
function New-Directory($path) {
$p, $components = $path -split '[\\/]'
$components | ForEach-Object {
$p = "$p\$_"
@ -1199,25 +1199,25 @@ function New-Directory($path) {
}
}
$null
}
$auPath = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU'
New-Directory $auPath
# set NoAutoUpdate.
# 0: Automatic Updates is enabled (default).
# 1: Automatic Updates is disabled.
New-ItemProperty `
}
$auPath = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU'
New-Directory $auPath
# set NoAutoUpdate.
# 0: Automatic Updates is enabled (default).
# 1: Automatic Updates is disabled.
New-ItemProperty `
-Path $auPath `
-Name NoAutoUpdate `
-Value 0 `
-PropertyType DWORD `
-Force `
| Out-Null
# set AUOptions.
# 1: Keep my computer up to date has been disabled in Automatic Updates.
# 2: Notify of download and installation.
# 3: Automatically download and notify of installation.
# 4: Automatically download and scheduled installation.
New-ItemProperty `
# set AUOptions.
# 1: Keep my computer up to date has been disabled in Automatic Updates.
# 2: Notify of download and installation.
# 3: Automatically download and notify of installation.
# 4: Automatically download and scheduled installation.
New-ItemProperty `
-Path $auPath `
-Name AUOptions `
-Value 3 `
@ -1225,13 +1225,13 @@ New-ItemProperty `
-Force `
| Out-Null
# disable Windows Update Delivery Optimization.
# NB this applies to Windows 10.
# 0: Disabled
# 1: PCs on my local network
# 3: PCs on my local network, and PCs on the Internet
$deliveryOptimizationPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DeliveryOptimization\Config'
if (Test-Path $deliveryOptimizationPath) {
# disable Windows Update Delivery Optimization.
# NB this applies to Windows 10.
# 0: Disabled
# 1: PCs on my local network
# 3: PCs on my local network, and PCs on the Internet
$deliveryOptimizationPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DeliveryOptimization\Config'
if (Test-Path $deliveryOptimizationPath) {
New-ItemProperty `
-Path $deliveryOptimizationPath `
-Name DODownloadMode `
@ -1239,20 +1239,20 @@ if (Test-Path $deliveryOptimizationPath) {
-PropertyType DWORD `
-Force `
| Out-Null
}
# Service tweaks for Windows Update
}
# Service tweaks for Windows Update
$services = @(
$services = @(
"BITS"
"wuauserv"
)
)
foreach ($service in $services) {
foreach ($service in $services) {
# -ErrorAction SilentlyContinue is so it doesn't write an error to stdout if a service doesn't exist
Write-Host "Setting $service StartupType to Automatic"
Get-Service -Name $service -ErrorAction SilentlyContinue | Set-Service -StartupType Automatic
}
}
Write-Host "Enabling driver offering through Windows Update..."
Remove-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\Device Metadata" -Name "PreventDeviceMetadataFromNetwork" -ErrorAction SilentlyContinue
Remove-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\DriverSearching" -Name "DontPromptForWindowsUpdate" -ErrorAction SilentlyContinue
@ -1341,10 +1341,10 @@ foreach ($service in $services) {
Get-BitsTransfer | Remove-BitsTransfer
Write-Host "10) Attempting to install the Windows Update Agent..."
if($arch -eq 64){
if ($arch -eq 64) {
wusa Windows8-RT-KB2937636-x64 /quiet
}
else{
else {
wusa Windows8-RT-KB2937636-x86 /quiet
}
@ -1364,29 +1364,29 @@ foreach ($service in $services) {
$Messageboxbody = ("Stock settings loaded.`n Please reboot your computer")
$MessageIcon = [System.Windows.MessageBoxImage]::Information
[System.Windows.MessageBox]::Show($Messageboxbody,$MessageboxTitle,$ButtonType,$MessageIcon)
})
[System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $MessageIcon)
})
$WPFUpdatesdisable.Add_Click({
# Source: https://github.com/rgl/windows-vagrant/blob/master/disable-windows-updates.ps1
Set-StrictMode -Version Latest
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'
trap {
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'
trap {
Write-Host
Write-Host "ERROR: $_"
Write-Host (($_.ScriptStackTrace -split '\r?\n') -replace '^(.*)$','ERROR: $1')
Write-Host (($_.Exception.ToString() -split '\r?\n') -replace '^(.*)$','ERROR EXCEPTION: $1')
Write-Host (($_.ScriptStackTrace -split '\r?\n') -replace '^(.*)$', 'ERROR: $1')
Write-Host (($_.Exception.ToString() -split '\r?\n') -replace '^(.*)$', 'ERROR EXCEPTION: $1')
Write-Host
Write-Host 'Sleeping for 60m to give you time to look around the virtual machine before self-destruction...'
Start-Sleep -Seconds (60*60)
Start-Sleep -Seconds (60 * 60)
Exit 1
}
}
# disable automatic updates.
# XXX this does not seem to work anymore.
# see How to configure automatic updates by using Group Policy or registry settings
# at https://support.microsoft.com/en-us/help/328010
function New-Directory($path) {
# disable automatic updates.
# XXX this does not seem to work anymore.
# see How to configure automatic updates by using Group Policy or registry settings
# at https://support.microsoft.com/en-us/help/328010
function New-Directory($path) {
$p, $components = $path -split '[\\/]'
$components | ForEach-Object {
$p = "$p\$_"
@ -1395,25 +1395,25 @@ function New-Directory($path) {
}
}
$null
}
$auPath = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU'
New-Directory $auPath
# set NoAutoUpdate.
# 0: Automatic Updates is enabled (default).
# 1: Automatic Updates is disabled.
New-ItemProperty `
}
$auPath = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU'
New-Directory $auPath
# set NoAutoUpdate.
# 0: Automatic Updates is enabled (default).
# 1: Automatic Updates is disabled.
New-ItemProperty `
-Path $auPath `
-Name NoAutoUpdate `
-Value 1 `
-PropertyType DWORD `
-Force `
| Out-Null
# set AUOptions.
# 1: Keep my computer up to date has been disabled in Automatic Updates.
# 2: Notify of download and installation.
# 3: Automatically download and notify of installation.
# 4: Automatically download and scheduled installation.
New-ItemProperty `
# set AUOptions.
# 1: Keep my computer up to date has been disabled in Automatic Updates.
# 2: Notify of download and installation.
# 3: Automatically download and notify of installation.
# 4: Automatically download and scheduled installation.
New-ItemProperty `
-Path $auPath `
-Name AUOptions `
-Value 1 `
@ -1421,13 +1421,13 @@ New-ItemProperty `
-Force `
| Out-Null
# disable Windows Update Delivery Optimization.
# NB this applies to Windows 10.
# 0: Disabled
# 1: PCs on my local network
# 3: PCs on my local network, and PCs on the Internet
$deliveryOptimizationPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DeliveryOptimization\Config'
if (Test-Path $deliveryOptimizationPath) {
# disable Windows Update Delivery Optimization.
# NB this applies to Windows 10.
# 0: Disabled
# 1: PCs on my local network
# 3: PCs on my local network, and PCs on the Internet
$deliveryOptimizationPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DeliveryOptimization\Config'
if (Test-Path $deliveryOptimizationPath) {
New-ItemProperty `
-Path $deliveryOptimizationPath `
-Name DODownloadMode `
@ -1435,22 +1435,22 @@ if (Test-Path $deliveryOptimizationPath) {
-PropertyType DWORD `
-Force `
| Out-Null
}
# Service tweaks for Windows Update
}
# Service tweaks for Windows Update
$services = @(
$services = @(
"BITS"
"wuauserv"
)
)
foreach ($service in $services) {
foreach ($service in $services) {
# -ErrorAction SilentlyContinue is so it doesn't write an error to stdout if a service doesn't exist
Write-Host "Setting $service StartupType to Disabled"
Get-Service -Name $service -ErrorAction SilentlyContinue | Set-Service -StartupType Disabled
}
}
})
})
$WPFUpdatessecurity.Add_Click({
Write-Host "Disabling driver offering through Windows Update..."
If (!(Test-Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\Device Metadata")) {
@ -1483,8 +1483,8 @@ $WPFUpdatessecurity.Add_Click({
$Messageboxbody = ("Recommended Update settings loaded")
$MessageIcon = [System.Windows.MessageBoxImage]::Information
[System.Windows.MessageBox]::Show($Messageboxbody,$MessageboxTitle,$ButtonType,$MessageIcon)
})
[System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $MessageIcon)
})
#===========================================================================
# Shows the form