Recently I’ve switched from Wordpress to Github Pages. I’m using Jekyll with Github pages, which means I get to write my posts in Markdown. Since I didnt’ want my old posts to go to waste entirely, I decided to see how I could ‘extract’ the posts from the Wordpress export I had, and put them in seperate Markdown-files.

I came up with the following script:

$_postPath = '.\_posts\'
$_oldWordPress = [xml](Get-Content "~\wordpress-backup.xml")

foreach ($_item in $ {
    $_postLayout = $_item.post_type
    $_postTitle = $_item.title
    if ($_postTitle -eq $null -or $_postTitle -like [string]::Empty) { $_postTitle = $_item.post_name }

    # Construct filename of the converted post, make sure anything other than letters and numbers is replaced by a dash.
    $_postFileName = "$(([datetime]::Parse($_item.post_date)).ToString('yyyy-MM-dd'))-$($_postTitle)" -replace '[^a-zA-Z0-9]','-'
    # $_postFileName

# We have to use a here-string to build up the markdown-file.
layout: $($_postLayout)
title: $($_postTitle)
$($_item.encoded.'#cdata-section' -replace "<[/]?code>","``````" -replace "<!--more-->","" -replace "<[/]?strong>","**" -replace "<[/]?em>","_" -replace '&gt;','>' -replace '&lt;','<')

"@ | Set-Content (Join-Path $_postPath "$($_postFileName).md")


There’s room for improvement, but I thought it’s a nice start, at least :)