shortcutter

shortcutter

e34e

param(

[Parameter(Mandatory=$true)]

[string]$SourceDirectory,

[Parameter(Mandatory=$true)]

[string]$ShortcutDirectory,

[Parameter(Mandatory=$false)]

[string[]]$ExecutableExtensions = @('.exe', '.com', '.bat', '.cmd'),

[Parameter(Mandatory=$false)]

[switch]$CreateSubdirectories

)


# Validate source directory exists

if (-not (Test-Path $SourceDirectory -PathType Container)) {

Write-Error "Source directory '$SourceDirectory' does not exist or is not a directory."

exit 1

}


# Create shortcut directory if it doesn't exist

if (-not (Test-Path $ShortcutDirectory)) {

New-Item -Path $ShortcutDirectory -ItemType Directory -Force | Out-Null

Write-Host "Created shortcut directory: $ShortcutDirectory" -ForegroundColor Green

}


# Create WScript.Shell COM object for creating shortcuts

$WshShell = New-Object -ComObject WScript.Shell


# Function to create a shortcut

function New-ExecutableShortcut {

param(

[string]$ExecutablePath,

[string]$ShortcutPath

)

try {

$Shortcut = $WshShell.CreateShortcut($ShortcutPath)

$Shortcut.TargetPath = $ExecutablePath

$Shortcut.WorkingDirectory = Split-Path $ExecutablePath -Parent

$Shortcut.IconLocation = "$ExecutablePath,0"

$Shortcut.Save()

Write-Host "Created shortcut: $ShortcutPath -> $ExecutablePath" -ForegroundColor Cyan

return $true

}

catch {

Write-Warning "Failed to create shortcut for '$ExecutablePath': $($_.Exception.Message)"

return $false

}

}


# Get all executable files recursively

Write-Host "Scanning directory '$SourceDirectory' for executable files..." -ForegroundColor Yellow


$ExecutableFiles = Get-ChildItem -Path $SourceDirectory -Recurse -File |

Where-Object { $_.Extension.ToLower() -in $ExecutableExtensions.ToLower() }


if ($ExecutableFiles.Count -eq 0) {

Write-Host "No executable files found in '$SourceDirectory'" -ForegroundColor Red

exit 0

}


Write-Host "Found $($ExecutableFiles.Count) executable files" -ForegroundColor Green


# Counter for statistics

$SuccessCount = 0

$FailureCount = 0


# Process each executable file

foreach ($ExeFile in $ExecutableFiles) {

$RelativePath = $ExeFile.FullName.Substring($SourceDirectory.Length).TrimStart('\')

$ShortcutName = [System.IO.Path]::GetFileNameWithoutExtension($ExeFile.Name) + ".lnk"

if ($CreateSubdirectories) {

# Preserve directory structure in shortcut directory

$RelativeDir = Split-Path $RelativePath -Parent

if ($RelativeDir) {

$ShortcutSubDir = Join-Path $ShortcutDirectory $RelativeDir

if (-not (Test-Path $ShortcutSubDir)) {

New-Item -Path $ShortcutSubDir -ItemType Directory -Force | Out-Null

}

$ShortcutPath = Join-Path $ShortcutSubDir $ShortcutName

} else {

$ShortcutPath = Join-Path $ShortcutDirectory $ShortcutName

}

} else {

# All shortcuts go directly to shortcut directory

# Handle duplicate names by adding a counter

$BaseShortcutPath = Join-Path $ShortcutDirectory $ShortcutName

$ShortcutPath = $BaseShortcutPath

$Counter = 1

while (Test-Path $ShortcutPath) {

$ShortcutName = [System.IO.Path]::GetFileNameWithoutExtension($ExeFile.Name) + "_$Counter.lnk"

$ShortcutPath = Join-Path $ShortcutDirectory $ShortcutName

$Counter++

}

}

# Create the shortcut

if (New-ExecutableShortcut -ExecutablePath $ExeFile.FullName -ShortcutPath $ShortcutPath) {

$SuccessCount++

} else {

$FailureCount++

}

}


# Display summary

Write-Host "`n--- Summary ---" -ForegroundColor Magenta

Write-Host "Total executables found: $($ExecutableFiles.Count)" -ForegroundColor White

Write-Host "Shortcuts created successfully: $SuccessCount" -ForegroundColor Green

Write-Host "Failed to create shortcuts: $FailureCount" -ForegroundColor Red

Write-Host "Shortcut directory: $ShortcutDirectory" -ForegroundColor White

Report Page