Here’s a PowerShell script you can copy-paste and run (as Administrator) to do a clean MongoDB setup on Windows 11, including:
✅ sets up MongoDB Server (fixes data/log paths)
✅ installs/starts the service
✅ installs MongoDB Shell (mongosh)
✅ installs MongoDB Compass (GUI)
✅ adds everything to your PATH
✅ verifies that MongoDB is running
🧠 MongoDB Full Setup Script for Windows 11
💡 Run this as Administrator — it handles service registration and installs software silently.
Copy and paste this into Notepad, then save it asC:\mongodb_full_setup.ps1
<#
MongoDB Full Auto Setup Script (Server + Shell + Compass)
Author: Rod (via GPT-5)
Tested on: Windows 11
#>
# === CONFIGURATION ===
$mongoVer = "8.2"
$serverPath = "C:\Program Files\MongoDB\Server\$mongoVer\bin"
$configPath = "$serverPath\mongod.cfg"
$mongoService = "MongoDB"
$dataDir = "C:\data\db"
$logDir = "C:\data\log"
$logFile = "$logDir\mongod.log"
$mongoshPath = "C:\Program Files\MongoDB\mongosh\bin\mongosh.exe"
$compassPath = "C:\Program Files\MongoDB Compass\MongoDBCompass.exe"
Write-Host "`n=== MongoDB Automated Setup ===" -ForegroundColor Cyan
# === 1. Create data and log directories ===
Write-Host "Creating MongoDB data and log directories..." -ForegroundColor Yellow
New-Item -ItemType Directory -Force -Path $dataDir | Out-Null
New-Item -ItemType Directory -Force -Path $logDir | Out-Null
# === 2. Fix mongod.cfg ===
if (Test-Path $configPath) {
Write-Host "Updating mongod.cfg with correct paths..." -ForegroundColor Yellow
$cfg = @"
systemLog:
destination: file
path: $logFile
logAppend: true
storage:
dbPath: $dataDir
net:
bindIp: 127.0.0.1
port: 27017
"@
Set-Content -Path $configPath -Value $cfg -Encoding UTF8
} else {
Write-Host "⚠️ Config file not found at $configPath. Please verify MongoDB Server installation." -ForegroundColor Red
}
# === 3. Install MongoDB Service ===
Write-Host "Registering MongoDB service..." -ForegroundColor Yellow
$mongodExe = "$serverPath\mongod.exe"
if (Test-Path $mongodExe) {
& $mongodExe --config $configPath --install
} else {
Write-Host "❌ mongod.exe not found. Please confirm MongoDB Server is installed." -ForegroundColor Red
exit
}
# === 4. Start MongoDB Service ===
Write-Host "Starting MongoDB service..." -ForegroundColor Yellow
Start-Service -Name $mongoService -ErrorAction SilentlyContinue
Start-Sleep -Seconds 3
Get-Service -Name $mongoService
# === 5. Install MongoDB Shell (mongosh) ===
if (-not (Test-Path $mongoshPath)) {
Write-Host "MongoDB Shell not found. Installing..." -ForegroundColor Cyan
$mongoshInstaller = "$env:TEMP\mongosh.msi"
Invoke-WebRequest -Uri "https://downloads.mongodb.com/compass/mongosh-2.3.5-x64.msi" -OutFile $mongoshInstaller
Start-Process msiexec.exe -Wait -ArgumentList "/i `"$mongoshInstaller`" /quiet /norestart"
} else {
Write-Host "MongoDB Shell already installed ✅" -ForegroundColor Green
}
# === 6. Install MongoDB Compass (GUI) ===
if (-not (Test-Path $compassPath)) {
Write-Host "Installing MongoDB Compass GUI..." -ForegroundColor Cyan
$compassInstaller = "$env:TEMP\mongodb-compass.msi"
Invoke-WebRequest -Uri "https://downloads.mongodb.com/compass/mongodb-compass-1.44.0-win32-x64.msi" -OutFile $compassInstaller
Start-Process msiexec.exe -Wait -ArgumentList "/i `"$compassInstaller`" /quiet /norestart"
} else {
Write-Host "MongoDB Compass already installed ✅" -ForegroundColor Green
}
# === 7. Add MongoDB to PATH ===
Write-Host "Adding MongoDB to PATH..." -ForegroundColor Yellow
$envPaths = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine)
if ($envPaths -notmatch [Regex]::Escape($serverPath)) {
[Environment]::SetEnvironmentVariable("Path", "$envPaths;$serverPath;C:\Program Files\MongoDB\mongosh\bin", [EnvironmentVariableTarget]::Machine)
}
# === 8. Final Test ===
Write-Host "`nVerifying MongoDB service..." -ForegroundColor Yellow
$svc = Get-Service -Name $mongoService
if ($svc.Status -eq "Running") {
Write-Host "✅ MongoDB service is running." -ForegroundColor Green
} else {
Write-Host "⚠️ MongoDB service not running — starting now..." -ForegroundColor Yellow
Start-Service MongoDB
}
Write-Host "`n=== ✅ MongoDB Setup Complete ===" -ForegroundColor Green
Write-Host "• MongoDB Server: $serverPath"
Write-Host "• Data Folder: $dataDir"
Write-Host "• Logs: $logFile"
Write-Host "• Shell: mongosh"
Write-Host "• GUI: MongoDB Compass"
Write-Host "`n👉 Open a NEW PowerShell window and run: mongosh" -ForegroundColor Cyan
Write-Host "👉 Or launch Compass from Start Menu: 'MongoDB Compass'" -ForegroundColor Cyan
⚙️ How to Run It
- Open PowerShell as Administrator
- Temporarily allow script execution:
Set-ExecutionPolicy RemoteSigned -Scope Process
3. Run the installer:
& "C:\mongodb_full_setup.ps1"
✅ After Installation
Check everything works:
Get-Service MongoDB
mongosh
You should see:
Connecting to: mongodb://127.0.0.1:27017/
Using MongoDB: 8.2.x
And in Start Menu → search MongoDB Compass to open the GUI.
→ You’re good to go 🎉
Login with the new admin user in mongosh
mongosh -u "admin" -p "MyPassword" --authenticationDatabase "admin"
Explanation of parameters:
-u "admin"→ your username-p "MyPassword"→ your password (enclose special characters in quotes)--authenticationDatabase "admin"→ MongoDB checks credentials in theadmindatabase