Overblog Tous les blogs Top blogs Technologie & Science Tous les blogs Technologie & Science
Editer l'article Suivre ce blog Administration + Créer mon blog
MENU

SkipOOBE-Windows

Publié le par damcuvelier

Script Powershell à lancer après avoir refusé l'OOBE à l'ouverture de session windows pour être sûr qu'il ne reviendra jamais:

#Requires -RunAsAdministrator
<#
  Applique SkipMachineOOBE/SkipUserOOBE = 1
  - Pour chaque ruche utilisateur chargée (HKU\<SID>)
  - Pour HKLM et HKCU
  - En garantissant le type REG_DWORD
#>

$ErrorActionPreference = 'Stop'

# --- Constantes ---
$UserRoot      = 'Registry::HKEY_USERS'
$TargetSubKey  = 'Software\Microsoft\Windows\CurrentVersion\OOBE'
$Values        = @('SkipMachineOOBE','SkipUserOOBE')

$HKLMPath      = 'Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\OOBE'
$HKCUPath      = 'Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\OOBE'
$StaticPaths   = @($HKLMPath, $HKCUPath)

# --- Helper: crée la clé si besoin ---
function Ensure-Key {
    param([string]$Path)
    if (-not (Test-Path -Path $Path)) {
        New-Item -Path $Path -Force | Out-Null
    }
}

# --- Helper: force une valeur REG_DWORD=1 (création ou correction de type) ---
function Set-Dword1 {
    param(
        [string]$Path,
        [string]$Name
    )
    # Valeur déjà présente ?
    $prop = Get-ItemProperty -Path $Path -Name $Name -ErrorAction SilentlyContinue

    if ($null -ne $prop) {
        # Récupère le type réel via .NET (sinon PS ne donne pas le type)
        $rk = [Microsoft.Win32.Registry]::Users.OpenSubKey($Path -replace '^Registry::HKEY_USERS\\','')
        if (-not $rk) {
            # Si ce n'est pas sous HKU, on bascule selon la ruche
            if ($Path -like 'Registry::HKEY_LOCAL_MACHINE*') {
                $rk = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($Path -replace '^Registry::HKEY_LOCAL_MACHINE\\','')
            } elseif ($Path -like 'Registry::HKEY_CURRENT_USER*') {
                $rk = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($Path -replace '^Registry::HKEY_CURRENT_USER\\','')
            }
        }

        $needRecreate = $true
        if ($rk) {
            $kind = $rk.GetValueKind($Name)
            if ($kind -eq [Microsoft.Win32.RegistryValueKind]::DWord) {
                $needRecreate = $false
            }
            $rk.Close()
        }

        if ($needRecreate) {
            # Supprime puis recrée en DWORD
            Remove-ItemProperty -Path $Path -Name $Name -ErrorAction SilentlyContinue
            New-ItemProperty -Path $Path -Name $Name -PropertyType DWord -Value 1 -Force | Out-Null
        } else {
            # Type déjà correct → on met à jour la valeur
            Set-ItemProperty -Path $Path -Name $Name -Value 1
        }
    } else {
        # Inexistant → création en DWORD
        New-ItemProperty -Path $Path -Name $Name -PropertyType DWord -Value 1 -Force | Out-Null
    }
}

Write-Host "==> Traitement HKU\<SID>\$TargetSubKey" -ForegroundColor Cyan

$users = Get-ChildItem $UserRoot |
    Where-Object { $_.PSChildName -match '^S-1-5-' -and $_.PSChildName -notmatch '_Classes$' }

foreach ($u in $users) {
    $sid = $u.PSChildName
    $path = Join-Path -Path "$UserRoot\$sid" -ChildPath $TargetSubKey
    try {
        Ensure-Key -Path $path
        foreach ($name in $Values) {
            Set-Dword1 -Path $path -Name $name
        }
        Write-Host "  [HKU\$sid] OK"
    } catch {
        Write-Warning "  [HKU\$sid] ERREUR: $($_.Exception.Message)"
    }
}

Write-Host "==> Traitement HKLM/HKCU" -ForegroundColor Cyan
foreach ($path in $StaticPaths) {
    try {
        Ensure-Key -Path $path
        foreach ($name in $Values) {
            Set-Dword1 -Path $path -Name $name
        }
        Write-Host "  [$path] OK"
    } catch {
        Write-Warning "  [$path] ERREUR: $($_.Exception.Message)"
    }
}

Write-Host "FINI."

 

Commenter cet article