🐧

Windows_Server

6 notes  •  Linux & Server Admin

Enable Python on IIS (Windows)

Configure IIS to interpret Python (.py) files via the CGI handler, so web requests to your Python scripts are executed server-side.

Prerequisites

  • Windows 10 or Windows Server with IIS installed
  • Python 2.7 or 3.x installed (64-bit recommended for 64-bit OS)
  • CGI feature enabled in IIS

Step 1 – Install Python

Download the 64-bit Python installer from python.org and install to C:\Python27\ (or your preferred path). Note the path to python.exe — you will need it in Step 4.

Step 2 – Enable IIS and CGI

  1. Open Windows Features (search "Turn Windows features on or off").
  2. Under Internet Information Services, enable:
    • Web Management Tools → IIS Management Console
    • World Wide Web Services → Application Development Features → CGI
  3. Click OK and wait for installation to complete.

Step 3 – Create a Website in IIS

  1. Open IIS Manager (search for "IIS" in the Start menu; run as Administrator).
  2. Right-click SitesAdd Website.
  3. Fill in the site name, physical path (your Python project folder, e.g. C:\PythonProject), and port.
  4. Right-click the new site → Edit Permissions → Security tab → Edit:
    • Add IUSR with Full Control.
    • Add IIS_IUSRS with Full Control.

Step 4 – Map Python as a CGI Script Handler

  1. Select your site in IIS Manager.
  2. Double-click Handler Mappings.
  3. In the Actions pane, click Add Script Map.
  4. Fill in the dialog:
    Request path : *.py
    Executable   : C:\Python27\python.exe %s %s
    Name         : Python Interpreter
  5. Click OK and confirm the dialog to allow the handler.

Step 5 – Configure the Application Pool

  1. Right-click your site → Manage Website → Advanced Settings.
  2. Set Application Pool to DefaultAppPool.
  3. Optionally enable Directory Browsing so you can browse and click .py files directly.

Step 6 – Verify

Create a minimal test script in your project folder:

print('Content-Type: text/plain')
print('')
print('Hello, world!')

Right-click the site → Manage Website → Browse. Your browser opens the file listing. Click MyPythonApp.py — you should see Hello, world! in the browser.

Troubleshooting

  • 500 Internal Server Error: Confirm the executable path in the script map is correct and includes %s %s.
  • 403 Forbidden: Check that IUSR and IIS_IUSRS have Full Control on the project folder.
  • CGI not listed in Handler Mappings: Re-enable the CGI Windows Feature and restart IIS.

Recover the GUI in Windows Server 2012 / 2012 R2

If the Windows Server 2012 GUI (Desktop Experience) was accidentally removed or failed to install, you can restore it from PowerShell.

Step 1 — Open PowerShell as Administrator

Win + X → Windows PowerShell (Admin)

Step 2 — Install the Desktop Experience Feature

Install-WindowsFeature Server-Gui-Shell, Server-Gui-Mgmt-Infra -Restart

Alternative — Install from Mounted ISO

If the feature files aren't cached locally, mount the Windows Server ISO and specify the source:

Install-WindowsFeature Server-Gui-Shell, Server-Gui-Mgmt-Infra `
  -Source D:\sources\sxs -Restart

Verify After Reboot

Get-WindowsFeature Server-Gui-Shell
# Should show: Installed

Fix Windows Server 2012 R2 Booting to Command Prompt Only

If Windows Server 2012 R2 boots to a command prompt instead of the GUI, the Desktop Experience feature may be disabled or corrupted.

Fix — Re-install GUI from Command Prompt

powershell
Install-WindowsFeature Server-Gui-Shell, Server-Gui-Mgmt-Infra -Restart

If PowerShell is Not Available

dism /online /enable-feature /featurename:Server-Gui-Shell /all /source:D:\sources\sxs

Set Default Boot to GUI Mode

bcdedit /set {current} optionsedit DISABLE
shutdown /r /t 0

Notes

  • Mount the Windows Server ISO and use D:\sources\sxs as the source if the feature files are missing.
  • After installing the GUI, set the default in Server Manager → Local Server → Properties.

Switch Between GUI and Server Core in Windows Server 2012

Windows Server 2012 and 2012 R2 allow switching between the full GUI (Desktop Experience) and Server Core (command-line only) without reinstalling.

Remove the GUI (Switch to Server Core)

Remove-WindowsFeature Server-Gui-Shell, Server-Gui-Mgmt-Infra -Restart

Restore the GUI (Switch to Desktop Experience)

Install-WindowsFeature Server-Gui-Shell, Server-Gui-Mgmt-Infra -Restart

Check Current State

Get-WindowsFeature Server-Gui-Shell

Notes

  • Server Core uses ~1–2 GB less RAM and has a smaller attack surface.
  • All roles (AD DS, DNS, DHCP, File Server) work in both modes.
  • Remote management via PowerShell and RSAT tools still works in Server Core mode.

Delete Files Older Than X Days in Windows (ForFiles / PowerShell)

How to automatically delete files older than a specified number of days using Windows built-in tools.

Method 1 — ForFiles Command

# Delete files older than 30 days in C:\Logs
forfiles /p "C:\Logs" /s /m *.* /d -30 /c "cmd /c del @path"

# Delete with confirmation (list only — don't delete)
forfiles /p "C:\Logs" /s /m *.* /d -30 /c "cmd /c echo @path @fdate"

Method 2 — PowerShell

# Delete files older than 30 days
$cutoff = (Get-Date).AddDays(-30)
Get-ChildItem -Path "C:\Logs" -Recurse -File |
  Where-Object { $_.LastWriteTime -lt $cutoff } |
  Remove-Item -Force

# Dry run (list without deleting)
Get-ChildItem -Path "C:\Logs" -Recurse -File |
  Where-Object { $_.LastWriteTime -lt $cutoff } |
  Select-Object FullName, LastWriteTime

Schedule with Task Scheduler

  1. Open Task Scheduler → Create Basic Task
  2. Set trigger: Daily
  3. Action: Start a program
  4. Program: powershell.exe
  5. Arguments: -File "C:\Scripts\cleanup.ps1"

Find and Kill Processes by Port in Windows

How to find which process is listening on a specific port and terminate it using Windows command-line tools.

Find the Process Using a Port

# List all listening ports with their PID
netstat -ano | findstr LISTENING

# Find a specific port (e.g., 8080)
netstat -ano | findstr :8080

Note the PID (last column) of the process using the port.

Kill the Process by PID

# Force kill by PID
taskkill /PID 1234 /F

# Find process name by PID first (optional)
tasklist | findstr 1234

PowerShell One-Liner

# Kill whatever is using port 8080
$pid = (netstat -ano | Select-String ":8080").ToString().Trim().Split()[-1]
Stop-Process -Id $pid -Force

Find and Kill by Process Name

# Kill all instances of a process
taskkill /IM python.exe /F
taskkill /IM node.exe /F