Post

MarkItDown Install Script

MarkItDown is a lightweight Python utility for converting various files to Markdown for use with LLMs and related text analysis pipelines.

Read more about MarkItDown: https://github.com/microsoft/markitdown

This is a Windows 10/11/Server installation script for MarkItDown. It installs the correct version of Python and adds a right click menu to convert files to markdown.


Run with:

1
powershell.exe -NoProfile -ExecutionPolicy Bypass -File .\Install-MarkItDown.ps1  

Save as: Install-MarkItDown.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# Install-MarkItDown.ps1
# Installs Microsoft MarkItDown for Windows using a dedicated Python virtual environment.
# Uses Python 3.10 through 3.13 only.
# Avoids Python 3.14 due to current dependency problems.
# Optionally installs FFmpeg to avoid pydub audio/video warnings.

[CmdletBinding()]
param(
    [string]$InstallRoot = "$env:USERPROFILE\MarkItDown",
    [switch]$SkipPythonInstall,
    [switch]$NoPathUpdate,
    [switch]$SkipFFmpegInstall
)

$ErrorActionPreference = "Stop"

function Write-Section {
    param([string]$Message)

    Write-Host ""
    Write-Host "==== $Message ====" -ForegroundColor Cyan
}

function Invoke-ExternalCommand {
    param(
        [Parameter(Mandatory = $true)]
        [string]$FilePath,

        [Parameter(Mandatory = $false)]
        [string[]]$Arguments = @(),

        [Parameter(Mandatory = $true)]
        [string]$FailureMessage,

        [switch]$IgnoreFailure
    )

    $stdoutFile = [System.IO.Path]::GetTempFileName()
    $stderrFile = [System.IO.Path]::GetTempFileName()

    try {
        $argLine = ($Arguments | ForEach-Object {
            if ($_ -match '\s') {
                '"' + ($_ -replace '"', '\"') + '"'
            }
            else {
                $_
            }
        }) -join " "

        $process = Start-Process `
            -FilePath $FilePath `
            -ArgumentList $argLine `
            -NoNewWindow `
            -Wait `
            -PassThru `
            -RedirectStandardOutput $stdoutFile `
            -RedirectStandardError $stderrFile

        $stdout = Get-Content -Path $stdoutFile -Raw -ErrorAction SilentlyContinue
        $stderr = Get-Content -Path $stderrFile -Raw -ErrorAction SilentlyContinue

        if ($stdout) {
            Write-Host $stdout.TrimEnd()
        }

        if ($stderr) {
            Write-Host $stderr.TrimEnd() -ForegroundColor Yellow
        }

        if ($process.ExitCode -ne 0 -and -not $IgnoreFailure) {
            throw "$FailureMessage Exit code: $($process.ExitCode)"
        }

        return $process.ExitCode
    }
    finally {
        Remove-Item -Path $stdoutFile, $stderrFile -Force -ErrorAction SilentlyContinue
    }
}

function Test-PythonExe {
    param([string]$PythonExe)

    try {
        $version = & $PythonExe -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}')" 2>$null

        if (-not $version) {
            return $false
        }

        $parts = $version.Split(".")
        $major = [int]$parts[0]
        $minor = [int]$parts[1]

        if ($major -eq 3 -and $minor -ge 10 -and $minor -le 13) {
            return $true
        }

        return $false
    }
    catch {
        return $false
    }
}

function Get-PythonVersion {
    param([string]$PythonExe)

    try {
        return & $PythonExe -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}')" 2>$null
    }
    catch {
        return $null
    }
}

function Get-GoodPython {
    Write-Section "Checking for Python 3.10 through 3.13"

    $preferredVersions = @("3.12", "3.13", "3.11", "3.10")
    $pyLauncher = Get-Command py.exe -ErrorAction SilentlyContinue

    if ($pyLauncher) {
        foreach ($ver in $preferredVersions) {
            try {
                $exe = & py "-$ver" -c "import sys; print(sys.executable)" 2>$null | Select-Object -First 1

                if ($exe -and (Test-Path $exe) -and (Test-PythonExe $exe)) {
                    $actualVersion = Get-PythonVersion -PythonExe $exe
                    Write-Host "Using Python $actualVersion at: $exe" -ForegroundColor Green
                    return $exe
                }
            }
            catch {
                # Keep checking other versions.
            }
        }
    }

    $pythonCmd = Get-Command python.exe -ErrorAction SilentlyContinue

    if ($pythonCmd -and (Test-PythonExe $pythonCmd.Source)) {
        $actualVersion = Get-PythonVersion -PythonExe $pythonCmd.Source
        Write-Host "Using Python $actualVersion at: $($pythonCmd.Source)" -ForegroundColor Green
        return $pythonCmd.Source
    }

    $knownPaths = @(
        "$env:LOCALAPPDATA\Programs\Python\Python312\python.exe",
        "$env:LOCALAPPDATA\Programs\Python\Python313\python.exe",
        "$env:LOCALAPPDATA\Programs\Python\Python311\python.exe",
        "$env:LOCALAPPDATA\Programs\Python\Python310\python.exe",
        "$env:ProgramFiles\Python312\python.exe",
        "$env:ProgramFiles\Python313\python.exe",
        "$env:ProgramFiles\Python311\python.exe",
        "$env:ProgramFiles\Python310\python.exe"
    )

    foreach ($path in $knownPaths) {
        if ((Test-Path $path) -and (Test-PythonExe $path)) {
            $actualVersion = Get-PythonVersion -PythonExe $path
            Write-Host "Using Python $actualVersion at: $path" -ForegroundColor Green
            return $path
        }
    }

    return $null
}

function Install-Python312 {
    if ($SkipPythonInstall) {
        throw "Python 3.10 through 3.13 was not found. Install Python 3.12 manually, then rerun this script."
    }

    $winget = Get-Command winget.exe -ErrorAction SilentlyContinue

    if (-not $winget) {
        throw "Python 3.10 through 3.13 was not found, and winget is not available. Install Python 3.12 manually, then rerun this script."
    }

    Write-Section "Installing Python 3.12 using winget"

    Invoke-ExternalCommand `
        -FilePath "winget.exe" `
        -Arguments @(
            "install",
            "--exact",
            "--id", "Python.Python.3.12",
            "--source", "winget",
            "--accept-package-agreements",
            "--accept-source-agreements"
        ) `
        -FailureMessage "winget failed to install Python 3.12."

    $machinePath = [Environment]::GetEnvironmentVariable("Path", "Machine")
    $userPath = [Environment]::GetEnvironmentVariable("Path", "User")
    $env:Path = "$machinePath;$userPath"

    return Get-GoodPython
}

function Test-FFmpeg {
    $ffmpeg = Get-Command ffmpeg.exe -ErrorAction SilentlyContinue

    if ($ffmpeg) {
        return $true
    }

    return $false
}

function Install-FFmpeg {
    if ($SkipFFmpegInstall) {
        Write-Host "Skipping FFmpeg install because -SkipFFmpegInstall was used." -ForegroundColor Yellow
        return
    }

    if (Test-FFmpeg) {
        Write-Host "FFmpeg already appears to be installed." -ForegroundColor Green
        return
    }

    $winget = Get-Command winget.exe -ErrorAction SilentlyContinue

    if (-not $winget) {
        Write-Host "winget is not available. Skipping FFmpeg install." -ForegroundColor Yellow
        Write-Host "MarkItDown can still work for PDFs, Word docs, Excel files, PowerPoint files, and text files."
        Write-Host "Audio/video conversion may complain until FFmpeg is installed."
        return
    }

    Write-Section "Installing FFmpeg using winget"

    $exitCode = Invoke-ExternalCommand `
        -FilePath "winget.exe" `
        -Arguments @(
            "install",
            "--exact",
            "--id", "Gyan.FFmpeg",
            "--source", "winget",
            "--accept-package-agreements",
            "--accept-source-agreements"
        ) `
        -FailureMessage "winget failed to install FFmpeg." `
        -IgnoreFailure

    if ($exitCode -ne 0) {
        Write-Host "FFmpeg install did not complete. Continuing anyway." -ForegroundColor Yellow
        Write-Host "This is not fatal for normal document conversion."
        return
    }

    $machinePath = [Environment]::GetEnvironmentVariable("Path", "Machine")
    $userPath = [Environment]::GetEnvironmentVariable("Path", "User")
    $env:Path = "$machinePath;$userPath"

    if (Test-FFmpeg) {
        Write-Host "FFmpeg installed successfully." -ForegroundColor Green
    }
    else {
        Write-Host "FFmpeg may have installed, but it was not found in PATH yet." -ForegroundColor Yellow
        Write-Host "Close and reopen PowerShell after this script finishes."
    }
}

function Add-ToUserPath {
    param([string]$Folder)

    $currentPath = [Environment]::GetEnvironmentVariable("Path", "User")

    if (-not $currentPath) {
        $currentPath = ""
    }

    $parts = $currentPath.Split(";") | Where-Object { $_ -and $_.Trim() -ne "" }

    $alreadyExists = $false

    foreach ($part in $parts) {
        if ($part.TrimEnd("\") -ieq $Folder.TrimEnd("\")) {
            $alreadyExists = $true
            break
        }
    }

    if (-not $alreadyExists) {
        $newPath = if ($currentPath.Trim()) { "$currentPath;$Folder" } else { $Folder }
        [Environment]::SetEnvironmentVariable("Path", $newPath, "User")
        $env:Path = "$Folder;$env:Path"

        Write-Host "Added to user PATH: $Folder" -ForegroundColor Green
    }
    else {
        Write-Host "Already in user PATH: $Folder" -ForegroundColor Green
    }
}

Write-Section "Starting MarkItDown install"

$PythonExe = Get-GoodPython

if (-not $PythonExe) {
    $PythonExe = Install-Python312
}

if (-not $PythonExe) {
    throw "Could not find or install a supported Python version. Use Python 3.12 for best results."
}

Write-Section "Creating install folder"

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

$VenvDir = Join-Path $InstallRoot ".venv"
$VenvPython = Join-Path $VenvDir "Scripts\python.exe"
$VenvMarkItDown = Join-Path $VenvDir "Scripts\markitdown.exe"

if (-not (Test-Path $VenvPython)) {
    Write-Host "Creating virtual environment at: $VenvDir"

    Invoke-ExternalCommand `
        -FilePath $PythonExe `
        -Arguments @("-m", "venv", $VenvDir) `
        -FailureMessage "Failed to create Python virtual environment."
}
else {
    Write-Host "Virtual environment already exists: $VenvDir"
}

Write-Section "Upgrading pip tools"

Invoke-ExternalCommand `
    -FilePath $VenvPython `
    -Arguments @("-m", "pip", "install", "--upgrade", "pip", "setuptools", "wheel", "--no-cache-dir") `
    -FailureMessage "pip tool upgrade failed."

Write-Section "Clearing pip cache"

Invoke-ExternalCommand `
    -FilePath $VenvPython `
    -Arguments @("-m", "pip", "cache", "purge") `
    -FailureMessage "pip cache purge failed." `
    -IgnoreFailure | Out-Null

Write-Section "Installing MarkItDown"

Invoke-ExternalCommand `
    -FilePath $VenvPython `
    -Arguments @("-m", "pip", "install", "--upgrade", "markitdown[all]", "--no-cache-dir") `
    -FailureMessage "MarkItDown pip install failed."

if (-not (Test-Path $VenvMarkItDown)) {
    throw "MarkItDown installed, but markitdown.exe was not found where expected: $VenvMarkItDown"
}

Write-Section "Installing optional FFmpeg dependency"

Install-FFmpeg

Write-Section "Creating command launcher"

$CmdPath = Join-Path $InstallRoot "markitdown.cmd"

$cmdContent = @"
@echo off
setlocal
"%~dp0.venv\Scripts\markitdown.exe" %*
endlocal
"@

Set-Content -Path $CmdPath -Value $cmdContent -Encoding ASCII

if (-not $NoPathUpdate) {
    Write-Section "Updating user PATH"
    Add-ToUserPath -Folder $InstallRoot
}

Write-Section "Testing MarkItDown"

$TestInput = Join-Path $InstallRoot "markitdown-test.txt"
$TestOutput = Join-Path $InstallRoot "markitdown-test.md"

"MarkItDown install test." | Set-Content -Path $TestInput -Encoding UTF8

# Suppress harmless dependency warnings during the basic text-file test.
$oldPythonWarnings = $env:PYTHONWARNINGS
$env:PYTHONWARNINGS = "ignore"

try {
    Invoke-ExternalCommand `
        -FilePath $CmdPath `
        -Arguments @($TestInput, "-o", $TestOutput) `
        -FailureMessage "MarkItDown test conversion failed."
}
finally {
    $env:PYTHONWARNINGS = $oldPythonWarnings
}

if (-not (Test-Path $TestOutput)) {
    throw "Test conversion failed. MarkItDown did not create the test markdown file."
}

Write-Host ""
Write-Host "MarkItDown installed successfully." -ForegroundColor Green
Write-Host ""
Write-Host "Install folder:"
Write-Host "  $InstallRoot"
Write-Host ""
Write-Host "Command launcher:"
Write-Host "  $CmdPath"
Write-Host ""
Write-Host "Test output:"
Write-Host "  $TestOutput"
Write-Host ""
Write-Host "Usage examples:"
Write-Host '  markitdown "C:\Path\To\File.pdf" -o "C:\Path\To\File.md"'
Write-Host '  markitdown "C:\Path\To\File.docx" -o "C:\Path\To\File.md"'
Write-Host '  markitdown "C:\Path\To\File.xlsx" -o "C:\Path\To\File.md"'
Write-Host '  markitdown "C:\Path\To\File.pptx" -o "C:\Path\To\File.md"'
Write-Host ""
Write-Host "If the markitdown command is not found right away, close and reopen PowerShell."
Write-Host "If FFmpeg was installed but not detected right away, close and reopen PowerShell."
This post is licensed under CC BY 4.0 by the author.