Skip to content

Instantly share code, notes, and snippets.

@sounisi5011
Last active November 25, 2023 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sounisi5011/b097e032f1d823a9a73e4c8b146ec62a to your computer and use it in GitHub Desktop.
Save sounisi5011/b097e032f1d823a9a73e4c8b146ec62a to your computer and use it in GitHub Desktop.
Windowsの初期設定を行うためのPowerShellスクリプト
#Requires -Version 5.0
#Requires -PSEdition Desktop
## 必要な場合にのみレジストリの値を更新する ##
function Set-ItemPropertyWhenNeeded() {
param (
[Parameter(Mandatory)]
[string]$LiteralPath,
[Parameter(Mandatory)]
[string]$Name,
[Parameter(Mandatory)]
$Value,
[Parameter(Mandatory)]
[string]$UpdateMessage,
[switch]$RunAsAdmin
)
# see https://bizlog.tech/get-reg-command/#toc3
if ((Get-Item -LiteralPath $LiteralPath).GetValue($Name) -ne $Value)
{
if ($RunAsAdmin)
{
# RunAsAdmin引数が指定されている場合は、管理者権限で更新する
# see https://qiita.com/sakekasunuts/items/63a4023887348722b416
# see https://docs.microsoft.com/ja-jp/powershell/module/microsoft.powershell.core/about/about_powershell_exe
# see https://docs.microsoft.com/ja-jp/powershell/module/microsoft.powershell.management/start-process
# see https://twitter.com/sounisi5011Prog/status/1558925321688080387
# see https://stackoverflow.com/a/25728864/4907315
Start-Process -FilePath powershell -Verb RunAs -Wait -WindowStyle hidden `
-ArgumentList "-Command ""Set-ItemProperty -LiteralPath '$LiteralPath' -Name '$Name' -Value $Value"""
}
else
{
Set-ItemProperty -LiteralPath $LiteralPath -Name $Name -Value $Value
}
# 値が更新されていたら、完了メッセージを表示する
if ((Get-Item -LiteralPath $LiteralPath).GetValue($Name) -eq $Value)
{
Write-Host $UpdateMessage
}
}
}
## Windows Package Managerがインストールされていなければ、インストールを促す ##
function Show-WingetInstallPopup {
# see https://trend-desk.com/archives/519
$wsobj = New-Object -ComObject wscript.shell
# see https://stackoverflow.com/a/11242455/4907315
while ((Get-Command -Name winget -ErrorAction SilentlyContinue) -eq $null) {
# see https://docs.microsoft.com/ja-jp/windows/uwp/publish/link-to-your-app
# see https://github.com/microsoft/winget-cli/blob/f95cdb9a596b1cf0d34aacf93721d45c114c8396/README.md#microsoft-store-recommended
[system.Diagnostics.Process]::start('ms-windows-store://pdp/?ProductId=9nblggh4nns1')
# Windows Package Managerのインストールを促すポップアップを表示
$result = $wsobj.Popup(
'wingetコマンドが見つかりません。Microsoft Storeから「アプリインストーラー」を導入してください',
-1,
'init.ps1',
5
)
# 「再試行」を押下しなかった場合は、終了する
if ($result -ne 4)
{
exit
}
}
}
Write-Host '[i] セットアップ中……'
## 拡張子を表示する ##
# see https://gist.github.com/shinyaa31/7fcb331285bc62ad6b872ae87359f3de
Set-ItemPropertyWhenNeeded 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced' 'HideFileExt' 0 `
-UpdateMessage '[i] 拡張子を表示する設定に変更しました'
## 隠しファイル、隠しフォルダ、隠しドライブを表示する ##
# see https://gist.github.com/shinyaa31/7fcb331285bc62ad6b872ae87359f3de
Set-ItemPropertyWhenNeeded 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced' 'Hidden' 1 `
-UpdateMessage '[i] 隠しファイルを表示する設定に変更しました'
## 高速スタートアップを無効にする ##
# see https://www.billionwallet.com/goods/windows10/win10_shutdown.html
Set-ItemPropertyWhenNeeded 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Power' 'HiberbootEnabled' 0 `
-UpdateMessage '[i] 高速スタートアップを無効化しました' -RunAsAdmin
## Windows Package Managerをインストールさせる ##
Write-Host '[i] wimgetコマンドを確認中……'
Show-WingetInstallPopup
## 必要なソフトをインストールする ##
Write-Host '[i] ソフトをインストール中……'
([ordered]@{
DeepL = 'DeepL.DeepL'
Discord = 'Discord.Discord'
Dropbox = 'Dropbox.Dropbox'
# Windows 10で表示がおかしいため、インストールしない
#Firefox = 'Mozilla.Firefox'
# Edgeがあるのでデフォルトではインストールしない
#'Google Chrome' = 'Google.Chrome'
KeePassXC = 'KeePassXCTeam.KeePassXC'
'Microsoft Visual C++ 2015-2022 再配布可能パッケージ' = 'Microsoft.VC++2015-2022Redist-x64' # KeePassXCの起動時にvcruntime140.dllが見つからないせいで失敗するため、これをインストールする
Steam = 'Valve.Steam'
VSCodium = 'VSCodium.VSCodium'
'OBS Studio' = 'OBSProject.OBSStudio'
}).GetEnumerator() | ForEach-Object {
# ソフトがWindows Package Managerによってインストールされているか確認する
if ((winget list --exact --id $_.value --accept-source-agreements | Out-String) -cnotmatch '\s+winget\s*$')
{
Write-Host ('[i] {0}をインストールします……' -f $_.key)
winget install --exact --id $_.value
}
}
Pause

Note

このスクリプトはOneDriveに入れておくべし

クリーンインストールしたWindowsを設定する際には、まず間違いなくMicrosoftアカウントにログインすることになります。すると、自動的にOneDriveも使用可能になります。このスクリプトをOneDriveに入れておくことで、Windowsの最低限のセットアップを行った直後に、インターネットブラウザーを開くことすらせず、初期設定を自動で行うことができます。

実行ポリシーを回避するため、init.ps1ファイル実行用のショートカットを作るべし

初期状態のWindowsは、実行ポリシーの影響でinit.ps1ファイルを実行できません。これを回避するためには、Set-ExecutionPolicyコマンドレットで実行ポリシーを変更する必要があります。しかし、設定の変更は、OS全体に影響を及ぼしてしまいます。一時的な初期設定スクリプトを実行するためだけに、不必要な設定変更は好ましくありません。

そこで、エクスプローラーで.ps1ファイルへのショートカット(.lnkファイル)を作成し、ショートカットのプロパティ内の「リンク先」を以下の値に変更します。

powershell -NoProfile -ExecutionPolicy RemoteSigned -File .\init.ps1

このショートカットをダブルクリックすることで、init.ps1ファイルを容易に実行できます。

参考:Powershellを楽に実行してもらうには - Qiita

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment