powershell レジストリ操作コマンド備忘録


# レジストリ操作powershell の備忘録

```powershell
powershell

function lskey ($key) { Get-ChildItem -Path "Registry::$key"}
function llkey ($key) { Get-ChildItem -Path "Registry::$key"}
function addkey ($key) { New-Item -Force -Path "Registry::$key" }
function delkey ($key) { Remove-Item -Path "Registry::$key" }
function delkeyR ($key) { Remove-Item -Force -Path "Registry::$key" }
function renkey ($beforekey, $afterkeyname) { Rename-Item -Path "Registry::$beforekey" -NewName $afterkeyname }
function cpkey ($srckey, $destkey) { Copy-Item -Path "Registry::$srckey" -Destination "Registry::$destkey" -Recurse }
function lsentry ($key) { Get-Item -Path "Registry::$key"}
function llentry ($key) { Get-Item -Path "Registry::$key" }
function addentry($key, $entry, $val) { New-ItemProperty -Force -Path "Registry::$key" -Name $entry -PropertyType String -Value $val }
function delentry($key, $entry) { Remove-ItemProperty -Path "Registry::$key" -Name $entry }

function addentry_force($key, $entry, $val) { 
    New-Item -Force -Path "Registry::$key"
    New-ItemProperty -Force -Path "Registry::$key" -Name $entry -PropertyType String -Value $val
}

function build_menu ($key, $label, $command, $iconpath) {
    $prefix = "Registry::$key"
    New-Item -Force -Path "$prefix\command"
    New-ItemProperty -Force -Path "$prefix" -Name "(default)" -PropertyType String -Value $label
    New-ItemProperty -Force -Path "$prefix\command" -Name "(default)" -PropertyType String -Value "$command ""%V"""
    if ( $iconpath -eq "" ) {
        New-ItemProperty -Force -Path "$prefix" -Name "Icon" -PropertyType String -Value $command
    } else {
        New-ItemProperty -Force -Path "$prefix" -Name "Icon" -PropertyType String -Value $iconpath
    }
}

function build_shellnew($extname, $val, $valuetype) {
    $prefix = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\.$extname"
    $key = "$prefix\ShellNew"
    New-Item -Force -Path "$key"
    New-ItemProperty -Force -Path $prefix -Name "(default)" -PropertyType String -Value $valuetype
    New-ItemProperty -Force -Path $key -Name "NullFile" -PropertyType String -Value ""
    New-ItemProperty -Force -Path $key -Name "NullFile" -PropertyType String -Value ""
    New-ItemProperty -Force -Path $key -Name "ItemName" -PropertyType ExpandString -Value $val
}

function build_filemenu($keyname, $label, $command, $iconpath="") {
    build_menu "HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\$keyname" $label $command $iconpath
}

function build_dirmenu($keyname, $label, $command, $iconpath="") {
    build_menu "HKEY_CURRENT_USER\SOFTWARE\Classes\Folder\shell\$keyname" $label $command $iconpath
    build_menu "HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\$keyname" $label $command $iconpath
}

function build_bothmenu($keyname, $label, $command, $iconpath="") {
    build_menu "HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\$keyname" $label $command $iconpath
    build_menu "HKEY_CURRENT_USER\SOFTWARE\Classes\Folder\shell\$keyname" $label $command $iconpath
    build_menu "HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\$keyname" $label $command $iconpath
}

function build_CLSID ($label, $clsid, $dllpath, $typelibid) {
    $prefix = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\CLSID\$clsid"
    
    New-Item -Force -Path "$prefix\InprocServer32"
    New-Item -Force -Path "$prefix\ProgID"
    New-Item -Force -Path "$prefix\Programmable"
    New-Item -Force -Path "$prefix\TypeLib"
    New-Item -Force -Path "$prefix\VersionIndependentProgID"
    
    New-ItemProperty -Force -Path "$prefix" -Name "(default)" -PropertyType String -Value "${label}Shell Class"
    New-ItemProperty -Force -Path "$prefix\InprocServer32" -Name "(default)" -PropertyType String -Value $dllpath
    New-ItemProperty -Force -Path "$prefix\InprocServer32" "ThreadingModel" -PropertyType String -Value "Apartment"
    New-ItemProperty -Force -Path "$prefix\ProgID" -Name "(default)" -PropertyType String -Value "ShellExtension.${label}Shell.1"
    New-ItemProperty -Force -Path "$prefix\Programmable" -Name "(default)" -PropertyType String -Value ""
    New-ItemProperty -Force -Path "$prefix\TypeLib" -Name "(default)" -PropertyType String -Value $typelibid
    New-ItemProperty -Force -Path "$prefix\VersionIndependentProgID" -Name "(default)" -PropertyType String -Value "ShellExtension.${label}Shell"
}

function build_filemenu_shellex($label, $clsid, $dllpath, $typelibid) {
    build_CLSID $label $clsid $dllpath $typelibid
    
    $key = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\*\shellex\ContextMenuHandlers\$label"
    New-Item -Force -Path $key
    New-ItemProperty -Force -Path $key -Name "(default)" -PropertyType String -Value $clsid
}

function build_dirmenu_shellex($label, $clsid, $dllpath, $typelibid) {
    build_CLSID $label $clsid $dllpath $typelibid
    
    $prefix = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory"
    New-Item -Force -Path "$prefix\Background\shellex\ContextMenuHandlers\$label"
    New-Item -Force -Path "$prefix\Background\shellex\DragDropHandlers\$label"
    New-Item -Force -Path "$prefix\shellex\ContextMenuHandlers\$label"
    New-Item -Force -Path "$prefix\shellex\DragDropHandlers\$label"
    
    New-ItemProperty -Force -Path "$prefix\Background\shellex\ContextMenuHandlers\$label" -Name "(default)" -PropertyType String -Value $clsid
    New-ItemProperty -Force -Path "$prefix\Background\shellex\DragDropHandlers\$label" -Name "(default)" -PropertyType String -Value $clsid
    New-ItemProperty -Force -Path "$prefix\shellex\ContextMenuHandlers\$label" -Name "(default)" -PropertyType String -Value $clsid
    New-ItemProperty -Force -Path "$prefix\shellex\DragDropHandlers\$label" -Name "(default)" -PropertyType String -Value $clsid
}

function build_bothmenu_shellex($label, $clsid, $dllpath, $typelibid) {
    build_dirmenu_shellex $label $clsid $dllpath $typelibid
    
    $key = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\*\shellex\ContextMenuHandlers\$label"
    New-Item -Force -Path $key
    New-ItemProperty -Force -Path $key -Name "(default)" -PropertyType String -Value $clsid
}

```

</details>

<details><summary>Chromeを既定にする関数</summary>

Edgeウザすぎなんよ

```powershell
function setdefault_chrome() {
    $prefix = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows"
    addentry "${prefix}\Shell\Associations\UrlAssociations\http\UserChoice" "ProgId" "ChromeHTML"
    addentry "${prefix}\Shell\Associations\UrlAssociations\https\UserChoice" "ProgId" "ChromeHTML"
    addentry "${prefix}\CurrentVersion\Explorer\FileExts\.htm\UserChoice" "ProgId" "ChromeHTML"
    addentry "${prefix}\CurrentVersion\Explorer\FileExts\.html\UserChoice" "ProgId" "ChromeHTML"
}

```

</details>


<details><summary>用法</summary>

参考 https://docs.microsoft.com/ja-function


```powershell
build_bothmenu "atashcase" "AtacheCase" "C:\Program Files (x86)\AttacheCase3\AttacheCase.exe"
build_bothmenu "VSCode" "VSCodeで開く" "Y:\usr\local\vscode\Code.exe"

build_filemenu "sakura" "SAKURAで開く" "Y:\usr\local\sakura\sakura.exe"
#build_filemenu "pathcopy" "パスをコピー" "%ComSpec% /Q /c Y:\cmd\fullpathcopy.bat" "Y:\share\icon\clippy.ico"

build_dirmenu "opencmd" "コマンドプロンプト" "%ComSpec% /Q /k pushd" "C:\Windows\system32\cmd.exe"
build_dirmenu "opencmd" "配下の一覧コピー" "%ComSpec% /Q /c ""dir /s /b | clip""" "Y:\share\icon\clippy.ico"
addentry "HKEY_CURRENT_USER\SOFTWARE\Classes\Folder\shell\opencmd\command" "(default)" "%ComSpec% /Q /c ""dir /B %V | clip"""
addentry "HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\opencmd\command" "(default)" "%ComSpec% /Q /c ""dir /B %V | clip"""

build_shellnew "txt" "@%SystemRoot%\system32\notepad.exe,-470" "txtfile"
addentry_force "HKEY_CURRENT_USER\SOFTWARE\Classes\.odt" "(default)" ""
addentry_force "HKEY_CURRENT_USER\SOFTWARE\Classes\.ods" "(default)" ""
addentry_force "HKEY_CURRENT_USER\SOFTWARE\Classes\.odp" "(default)" ""
addentry_force "HKEY_CURRENT_USER\SOFTWARE\Classes\.odg" "(default)" ""
addentry_force "HKEY_CURRENT_USER\SOFTWARE\Classes\.pub" "(default)" ""

build_bothmenu_Ex "WinMerge" "{4E716236-AA30-4C65-B225-D68BBA81E9C2}" "Y:\usr\local\WinMerge\ShellExtensionX64.dll" "{06029E17-28B5-456A-B866-4E79D98612FD}"
build_bothmenu_Ex "PathCopy" "{82CB99A2-2F18-4D5D-9476-54347E3B6720}" "Y:\usr\local\PathCopy\PCC64.dll" "{2E3829C9-CB67-4C81-B304-B6FE22816E4C}"


```