# ============================================
# 🔧 سكربت استعادة اختصارات البرامج في ويندوز 10/11
# النسخة المطوّرة - إعداد GPT-5
# ============================================
Write-Host "`n🚀 جاري فحص النظام لإعادة بناء اختصارات البرامج..." -ForegroundColor Cyan
# المسارات الرئيسية
$StartMenuAllUsers = "$env:ProgramData\Microsoft\Windows\Start Menu\Programs"
$StartMenuUser = "$env:AppData\Microsoft\Windows\Start Menu\Programs"
$DesktopPath = [Environment]::GetFolderPath("Desktop")
# إنشاء كائن لإنشاء الاختصارات
$WScriptShell = New-Object -ComObject WScript.Shell
# التأكد من وجود مجلدات الاختصارات
foreach ($path in @($StartMenuAllUsers, $StartMenuUser)) {
    if (!(Test-Path $path)) {
        New-Item -ItemType Directory -Path $path | Out-Null
    }
}
# --------------------------------------------
# 🧩 الخطوة 1: استعادة اختصارات تطبيقات UWP المثبتة
# --------------------------------------------
Write-Host "`n[1/3] البحث عن تطبيقات النظام المثبتة..." -ForegroundColor Yellow
$Apps = Get-StartApps
foreach ($App in $Apps) {
    $ShortcutName = ($App.Name -replace '[\\/:*?"<>|]', '_') + ".lnk"
    $ShortcutPath = Join-Path $StartMenuAllUsers $ShortcutName
    if (!(Test-Path $ShortcutPath)) {
        try {
            $Shortcut = $WScriptShell.CreateShortcut($ShortcutPath)
            $Shortcut.TargetPath = $App.AppID
            $Shortcut.Save()
            Write-Host "✅ تم إنشاء اختصار لتطبيق: $($App.Name)" -ForegroundColor Green
        } catch {
            Write-Host "⚠️ فشل إنشاء اختصار لـ: $($App.Name)" -ForegroundColor Yellow
        }
    }
}
# --------------------------------------------
# 🧭 الخطوة 2: فحص مجلدات Program Files
# --------------------------------------------
Write-Host "`n[2/3] البحث في مجلدات البرامج المثبتة..." -ForegroundColor Yellow
$ProgramFolders = @(
    "C:\Program Files",
    "C:\Program Files (x86)"
)
$ExeFiles = @()
foreach ($folder in $ProgramFolders) {
    if (Test-Path $folder) {
        $ExeFiles += Get-ChildItem -Path $folder -Filter *.exe -Recurse -ErrorAction SilentlyContinue | 
                     Where-Object { $_.FullName -notmatch "WindowsApps|AppInstaller|Windows Defender|Microsoft Office" }
    }
}
# --------------------------------------------
# 🖇️ الخطوة 3: إنشاء اختصارات مفقودة للبرامج
# --------------------------------------------
Write-Host "`n[3/3] إنشاء اختصارات البرامج المكتشفة..." -ForegroundColor Yellow
foreach ($exe in $ExeFiles) {
    $AppName = ($exe.BaseName -replace '[\\/:*?"<>|]', '_')
    $ShortcutName = "$AppName.lnk"
    $ShortcutPathStart = Join-Path $StartMenuAllUsers $ShortcutName
    $ShortcutPathDesktop = Join-Path $DesktopPath $ShortcutName
    if (!(Test-Path $ShortcutPathStart)) {
        try {
            $Shortcut = $WScriptShell.CreateShortcut($ShortcutPathStart)
            $Shortcut.TargetPath = $exe.FullName
            $Shortcut.WorkingDirectory = Split-Path $exe.FullName
            $Shortcut.Save()
            Write-Host "📎 تمت إضافة اختصار في Start Menu: $AppName" -ForegroundColor Green
        } catch {
            Write-Host "⚠️ لم يتم إنشاء اختصار لـ: $AppName" -ForegroundColor Yellow
        }
    }
    if (!(Test-Path $ShortcutPathDesktop)) {
        try {
            $Shortcut = $WScriptShell.CreateShortcut($ShortcutPathDesktop)
            $Shortcut.TargetPath = $exe.FullName
            $Shortcut.WorkingDirectory = Split-Path $exe.FullName
            $Shortcut.Save()
            Write-Host "🖥️ تمت إضافة اختصار على سطح المكتب: $AppName" -ForegroundColor Green
        } catch {
            Write-Host "⚠️ لم يتم إنشاء اختصار سطح المكتب لـ: $AppName" -ForegroundColor Yellow
        }
    }
}
Write-Host "`n✅ تم الانتهاء من استعادة جميع الاختصارات بنجاح!" -ForegroundColor Cyan
Pause