Merhabalar
KONU ANLATIMI :
KOD İÇERİĞİ ( LinkVePathYöneticisi.ps1 ) :
Güle güle kullanın....
KONU ANLATIMI :
KOD İÇERİĞİ ( LinkVePathYöneticisi.ps1 ) :
Kod:
# =====================================================================
# LinkVePathYoneticisi.ps1
# Türkçe karakter desteği ve UTF-8 konsol çıktısı eklendi.
# =====================================================================
# KONSOL KODLAMASINI UTF-8'E ZORLA (Türkçe karakterlerin ekranda doğru görünmesi için)
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8
# 1. ADIM: YÖNETİCİ KONTROLÜ (Sembolik Link ve Sistem PATH için şarttır)
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-Host "[BİLGİ] Bu işlem Yönetici yetkisi gerektirir. Betik yönetici olarak yeniden başlatılıyor..." -ForegroundColor Yellow
Start-Process powershell.exe -Verb RunAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`""
exit
}
# Ortam değişikliklerini Windows'a bildirmek için API çağrısı (Restart gerektirmez)
Add-Type -Namespace Win32 -Name NativeMethods -MemberDefinition @"
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(
IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam,
uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
"@
function Broadcast-EnvChange {
$HWND_BROADCAST = [IntPtr]0xffff
$WM_SETTINGCHANGE = 0x1a
$result = [UIntPtr]::Zero
[Win32.NativeMethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE, [UIntPtr]::Zero, "Environment", 2, 5000, [ref]$result) | Out-Null
}
Clear-Host
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " KISAYOL, SEMBOLİK LİNK VE PATH YÖNETİCİSİ" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# 2. ADIM: HEDEF YOLU ALMA
do {
$hedefYol = Read-Host "Lütfen yazılımın (.exe) veya klasörün TAM YOLUNU girin"
if (-not (Test-Path $hedefYol)) {
Write-Host "[HATA] Girdiğiniz yol bulunamadı. Lütfen tekrar deneyin." -ForegroundColor Red
}
} while (-not (Test-Path $hedefYol))
$item = Get-Item $hedefYol
$isFile = -not $item.PSIsContainer
# Eğer dosya seçildiyse, PATH'e eklenecek yol dosyanın bulunduğu klasördür.
$pathEklenecekKonum = if ($isFile) { $item.DirectoryName } else { $item.FullName }
Write-Host "[BAŞARILI] Hedef bulundu: $hedefYol" -ForegroundColor Green
if ($isFile) {
Write-Host "[BİLGİ] Bir dosya seçtiniz. PATH değişkenine dosyanın kendisi değil, bulunduğu klasör ($pathEklenecekKonum) eklenecektir." -ForegroundColor Yellow
}
Write-Host ""
# 3. ADIM: KISAYOL VE LİNK KONUMUNU ALMA
do {
$kaydedilecekKlasor = Read-Host "Kısayol ve Sembolik Link'in oluşturulacağı KLASÖRÜN tam yolunu girin"
if (-not (Test-Path $kaydedilecekKlasor)) {
$olustur = Read-Host "[UYARI] Klasör bulunamadı. Oluşturmak ister misiniz? (e/h)"
if ($olustur -eq 'e' -or $olustur -eq 'E') {
New-Item -Path $kaydedilecekKlasor -ItemType Directory -Force | Out-Null
Write-Host "Klasör oluşturuldu." -ForegroundColor Green
} else {
Write-Host "İşlem iptal edildi." -ForegroundColor Red
exit
}
}
} while (-not (Test-Path $kaydedilecekKlasor))
$linkIsmi = Read-Host "Oluşturulacak linklerin/kısayolun İSMİNİ girin (uzantısız, örneğin: BenimYazilimim)"
# 4. ADIM: STANDART KISAYOL (.lnk) OLUŞTURMA
$ws = New-Object -ComObject WScript.Shell
$shortcutPath = Join-Path $kaydedilecekKlasor "$linkIsmi.lnk"
$shortcut = $ws.CreateShortcut($shortcutPath)
$shortcut.TargetPath = $hedefYol
if ($isFile) {
$shortcut.WorkingDirectory = $item.DirectoryName
} else {
$shortcut.WorkingDirectory = $item.FullName
}
$shortcut.Save()
Write-Host "[BAŞARILI] Standart kısayol (.lnk) oluşturuldu: $shortcutPath" -ForegroundColor Green
# 5. ADIM: SEMBOLİK LİNK (SYMLINK) OLUŞTURMA
$symlinkPath = Join-Path $kaydedilecekKlasor $linkIsmi
try {
# Eğer aynı isimde bir şey varsa üzerine yazmasını engellemek için kontrol
if (Test-Path $symlinkPath) { Remove-Item $symlinkPath -Force }
New-Item -Path $symlinkPath -ItemType SymbolicLink -Value $hedefYol | Out-Null
Write-Host "[BAŞARILI] Sembolik Link (Symlink) oluşturuldu: $symlinkPath" -ForegroundColor Green
} catch {
Write-Host "[HATA] Sembolik link oluşturulurken hata: $_" -ForegroundColor Red
}
Write-Host ""
# 6. ADIM: PATH DEĞİŞKENİNE EKLEME SORGUSU
$ekle = Read-Host "Bu konumu ($pathEklenecekKonum) Kullanıcı ve Sistem PATH ortam değişkenlerine eklemek ister misiniz? (e/h)"
if ($ekle -eq 'e' -or $ekle -eq 'E') {
Write-Host "PATH değişkenleri kontrol ediliyor ve güncelleniyor..." -ForegroundColor Cyan
# Mevcut PATH'leri çek
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
$sysPath = [Environment]::GetEnvironmentVariable("Path", "Machine")
$degisti = $false
# Kullanıcı PATH'ine ekle
$userPaths = $userPath -split ';' | Where-Object { $_ -ne "" }
if ($userPaths -notcontains $pathEklenecekKonum) {
$newUserPath = if ([string]::IsNullOrWhiteSpace($userPath)) { $pathEklenecekKonum } else { "$userPath;$pathEklenecekKonum" }
[Environment]::SetEnvironmentVariable("Path", $newUserPath, "User")
Write-Host "[BAŞARILI] Kullanıcı PATH değişkenine eklendi." -ForegroundColor Green
$degisti = $true
} else {
Write-Host "[BİLGİ] Konum zaten Kullanıcı PATH değişkeninde mevcut." -ForegroundColor Yellow
}
# Sistem PATH'ine ekle
$sysPaths = $sysPath -split ';' | Where-Object { $_ -ne "" }
if ($sysPaths -notcontains $pathEklenecekKonum) {
$newSysPath = if ([string]::IsNullOrWhiteSpace($sysPath)) { $pathEklenecekKonum } else { "$sysPath;$pathEklenecekKonum" }
[Environment]::SetEnvironmentVariable("Path", $newSysPath, "Machine")
Write-Host "[BAŞARILI] Sistem PATH değişkenine eklendi." -ForegroundColor Green
$degisti = $true
} else {
Write-Host "[BİLGİ] Konum zaten Sistem PATH değişkeninde mevcut." -ForegroundColor Yellow
}
# Windows'a değişikliği bildir (Restart gereksinimini kaldırır)
if ($degisti) {
Broadcast-EnvChange
Write-Host "[BİLGİ] Windows ortam değişikliklerinden haberdar edildi. Yeni CMD/PowerShell pencerelerinde path hemen geçerli olacaktır." -ForegroundColor Cyan
}
} else {
Write-Host "[BİLGİ] PATH işlemi atlandı." -ForegroundColor Yellow
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " İŞLEM TAMAMLANDI. PENCEREYİ KAPATABİLİRSİNİZ." -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Read-Host "Çıkmak için ENTER'a basın"
Güle güle kullanın....