Skip to content

Instantly share code, notes, and snippets.

@spencershepard
Created March 28, 2025 19:04
Show Gist options
  • Save spencershepard/45cbb5f72a4952187d40934446ad552e to your computer and use it in GitHub Desktop.
Save spencershepard/45cbb5f72a4952187d40934446ad552e to your computer and use it in GitHub Desktop.
(Windows) Automatically run all installers in a folder with elevated permissions
Echo OFF
REM The next command sets the working directory to the directory of the script (ie instead of C:/Windows/System32 when run as admin)
cd /d "%~dp0"
powershell -NoProfile -ExecutionPolicy Bypass -File "batchinstaller.ps1"
PAUSE > NUL
# PowerShell script to install software/driver packages in the current folder
$installer_extensions = @(
".exe",
".msi",
".bat",
".cmd"
)
$excluded_files = @(
"batchinstaller.cmd",
"skipme.bat"
)
$stop_on_error = $false
# Set the current directory to the script's location
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location -Path $scriptDir
# Get all files in the current directory with the specified extensions
$installerFiles = Get-ChildItem -Path $scriptDir -File | Where-Object { $_.Extension -in $installer_extensions }
# Remove the excluded files from the list
$installerFiles = $installerFiles | Where-Object { $_.Name -notin $excluded_files }
# Check if there are any installer files to process
if ($installerFiles.Count -eq 0) {
Write-Host "No installer files found in the current directory." -ForegroundColor Yellow
Exit
}
Write-Host "Found the following installer files:" -ForegroundColor Green
foreach ($file in $installerFiles) {
Write-Host " - $($file.Name)" -ForegroundColor Cyan
}
Write-Host ""
Read-Host "Press Enter to continue or Ctrl+C to cancel"
# Check if the script is running as administrator
If (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Host "ERROR. Please run this script as an administrator." -ForegroundColor Red
Pause
Exit
}
# Run each installer file as administrator
$errors = @()
foreach ($app in $installerFiles) {
try {
Write-Host "Running $app as administrator ..." -ForegroundColor Blue
Start-Process -FilePath $app -Verb RunAs -Wait
} catch {
Write-Host "Error installing $app" -ForegroundColor Red
$errors += $_
Write-Host "Error message: $($_.Exception.Message)" -ForegroundColor Yellow
if ($stop_on_error) {
Write-Host "Stopping installation due to error. " -ForegroundColor Red
break
} else {
Write-Host "Continuing after error..." -ForegroundColor Yellow
}
}
}
# Display result
Write-Host ""
if ($errors.Count -gt 0) {
Write-Host "Finished with errors" -ForegroundColor White -BackgroundColor Red
} else {
Write-Host "All installations completed successfully!" -ForegroundColor White -BackgroundColor Green
}
Pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment