捐血一袋救人一命

2021年3月2日 星期二

使用 Powershell 設定電腦環境

因為經常需要設定電腦,所以寫了個程式來設定電腦的環境,免得經常要手動設定,還容易忘記漏設定。

Param ([switch]$Verbose)
$VerboseStatus = $VerbosePreference
If($Verbose){
    $VerbosePreference = "Continue"
}
If(-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")){
    $ScriptFullPath = $MyInvocation.MyCommand.Definition
$arguments = "& '" + $ScriptFullPath + "'"
    # 另外以管理者身分執行 PowerShell,並載入 Script 來執行
Start-Process powershell -Verb runAs -ArgumentList $arguments
    # 中斷後面程式指令
Break
}
Write-Verbose "伺服器管理員不要每次開機都顯示"
New-ItemProperty -Path HKCU:\Software\Microsoft\ServerManager -Name DoNotOpenServerManagerAtLogon -PropertyType DWORD -Value “0x1” –Force | Out-Null
Write-Verbose "關閉 IE 增強安全性"
$AdminKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}"
Set-ItemProperty -Path $AdminKey -Name "IsInstalled" -Value 0
$UserKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}"
Set-ItemProperty -Path $UserKey -Name "IsInstalled" -Value 0
Stop-Process -Name Explorer
Write-Verbose "關閉使用者存取控制(UAC)"
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" -Name "ConsentPromptBehaviorAdmin" -Value 00000000
Write-Verbose "設定微軟注音輸入法,預設為英文模式"
Set-ItemProperty "HKCU:\Software\Microsoft\IME\15.0\IMETC" -Name "Default Input Mode" -Value 0x00000001
Write-Verbose "設定電源模式-高效能"
& "powercfg.exe" /setactive 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
Write-Verbose "設定電源模式-高效能-永不關閉螢幕"
& "powercfg.exe" -change -monitor-timeout-ac 0
Write-Verbose "開機自動登入系統"
$usrname = 'user'
$password = 'mypasswd'
$RegistryLocation = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
Set-ItemProperty $RegistryLocation -Name 'AutoAdminLogon' -Value '1'
Set-ItemProperty $RegistryLocation -Name 'DefaultUsername' -Value "$usrname"
Set-ItemProperty $RegistryLocation -Name 'DefaultPassword' -Value "$password"
Write-Verbose "設定時區、指定校時伺服器並校時"
$TimeZone = "Taipei Standard Time"
$NTPServer1 = "time1.google.com"
$NTPServer2 = "time.windows.com"
$NTPServer3 = "time.nist.gov"
Set-TimeZone -Id $TimeZone -PassThru | Out-Null
Push-Location
Set-Location HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers
Set-ItemProperty . 0 $NTPServer1
Set-ItemProperty . 1 $NTPServer2
Set-ItemProperty . 2 $NTPServer3
Set-ItemProperty . "(Default)" "0"
Set-Location HKLM:\SYSTEM\CurrentControlSet\services\W32Time\Parameters
Set-ItemProperty . NtpServer $NTPServer1
Pop-Location
Stop-Service w32time
Start-Service w32time
Write-Verbose "變更網路區域為 Private (Not Public)"
$NetAdapter = (Get-NetConnectionProfile -IPv4Connectivity Internet).InterfaceIndex
Set-NetConnectionProfile -InterfaceIndex $NetAdapter -NetworkCategory Private
$VerbosePreference = $VerboseStatus
If($Host.Name -eq "ConsoleHost"){
    Pause
}

0 意見: