[DISCUSSION] Windows 11 Build 26x00 (PC) [24/25H2 Retail- GE-release]

Discussion in 'Windows 11' started by Enthousiast, Apr 3, 2024.

  1. Tito

    Tito Admin / Adviser
    Staff Member

    Nov 30, 2009
    19,041
    19,756
    340
    Code:
    @echo off
    setlocal enabledelayedexpansion
    chcp 65001 >nul
    
    set "input=%*"
    
    if "%input%"=="" (
        echo Usage: SetDefaultUserFolder.cmd ^<value^>
        echo Example: SetDefaultUserFolder.cmd MyFolderName
        exit /b 1
    )
    
    :: Now call PowerShell for detailed Unicode and reserved name validation
    powershell -NoProfile -Command ^
        "$name = '%input%'.Trim();" ^
        "$invalid = [System.IO.Path]::GetInvalidFileNameChars() + @('@', '.', ';', '=', ',', '+');" ^
        "if ([string]::IsNullOrWhiteSpace($name)) { Write-Host '[ERROR] Name is empty or whitespace.'; exit 1 }" ^
        "elseif ($name.Length -gt 16) { Write-Host '[ERROR] Name exceeds 16 character limit.'; exit 2 }" ^
        "elseif ($name.EndsWith(' ') -or $name.EndsWith('.')) { Write-Host '[ERROR] Name cannot end with space or dot.'; exit 1 }" ^
        "elseif ('CON','PRN','AUX','NUL','COM1','COM2','COM3','COM4','COM5','COM6','COM7','COM8','COM9','LPT1','LPT2','LPT3','LPT4','LPT5','LPT6','LPT7','LPT8','LPT9' -contains $name.ToUpper()) { Write-Host \"[ERROR] '$name' is a reserved name.\"; exit 1 }" ^
        "elseif ($invalidChar = $name.ToCharArray() | Where-Object { $invalid -contains $_ } | Select-Object -First 1) { Write-Host \"[ERROR] Invalid character found in name: '$invalidChar'\"; exit 1 }" ^
        "else { Write-Host \"[OK] '$name' is a valid folder name.\" }"
    
    if errorlevel 1 (
        echo [ERROR] Folder name validation failed in PowerShell.
        exit /b 4
    )
    
    :: Write to registry
    reg add HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\UserOOBE /v SetSAMFolderName /t REG_SZ /d "%input%" /f >nul
    if %errorlevel% EQU 0 (
        echo [SUCCESS] Default user folder set to: %input%
    ) else (
        echo [ERROR] Failed to set registry value. %errorlevel%
        exit /b 5
    )
    
    exit /b 0
    
     
  2. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    17,885
    95,439
    340
    Why enabledelayedexpansion if you don't intend to use it? :rolleyes:
    "%input%" comparison might fail if it contains invalid characters
     
  3. BaffledSoftware

    BaffledSoftware MDL Member

    Mar 19, 2019
    109
    12
    10
    Exactly! Hence, why I asked how can one have sharp edges?

    Also, how can one have multiple town weather forecasts (live tiles) on the start menu like on W10? Is that possible on W11?
     
  4. 12 lb Turkey

    12 lb Turkey MDL Member

    Nov 24, 2022
    151
    95
    10
    Are they still allowing interns to write PS code?

    -contains
    is case-insensitive, so why bother with $name.ToUpper() ?