PowerShell: pattern matching

Here, I am defining a hex character, as 1 of A-F (case insensitive) or 1 of 0-9. The format is no different from any other common Regex/pattern matching implementation:

Then, we want to look for the pattern anywhere within the passed line (\b..\b – word boundary). Still in that $guidPattern string, we are saying that the pattern starts with exactly 8 of the hex characters we defined earlier, followed by a literal ‘-‘, etc… ending up with exactly 12 characters, then a word boundary.

And finally the whole thing in a PowerShell script:

Given a set of files, for each file, if a line does not contain the pattern, then
write the line to name-copy-plus-suffix of the file, else do not write the line.
$root = "C:\Sandbox"
cd $root
$fileSet = Get-ChildItem -Path .\*.xml -Name

$hexChar = "[A-Fa-f0-9]"

$guidPattern = "\b$hexChar{8}-$hexChar{4}-$hexChar{4}-$hexChar{4}-$hexChar{12}\b"

$fileSet | foreach {
$fileName = "$root\$_"
$fileToSave = "$fileName" + ".sansGuid"
Write-Host "Processing $fileName"
$reader = New-Object -TypeName System.IO.StreamReader("$fileName")
$writer = [System.IO.StreamWriter] $fileToSave

while (($line = $reader.ReadLine()) -ne $null) {
if ($line -notMatch $guidPattern) {


PowerShell: splitting an input file and saving to wav format in chunks

On 4 out of 5 days, I have a car journey that is between 0.75 and 1.25 hours. I want to be able to take a free (e.g. Project Gutenberg) book, or at least a DRM free book, split it into sections, and create an audio file from each section.
Let’s say that the following is my entirety of my book:


I want to read/hear in sections: lines 1 and 2 (section 1), lines 3 and 4 (section 2), lines 5 and 6 (section 3), line 7 (section 4), giving this:


This PowerShell is one way to do that (Although I write the split text back out to disk and then read it back in, that step could be removed).


function Get-FileName($extension = "txt") {
 "{0}{1}_{2}.{3}" -f ($outputRootDir, $outputFileNamePrefix, $chunk, $extension)
function Write-WavFile() {
 $speech = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
 $speech.SelectVoice("Microsoft Hazel Desktop")
 $textToSpeak = Get-Content -Path $(Get-FileName) -Encoding UTF8
 $speech.SetOutputToWaveFile($(Get-FileName "wav"))
 $speech = $null
function Split-File (
 $fileToSplit = 'C:\Temp\pandp.txt',
 $splitMarker = "SPLITHERE",
 $outputFileNamePrefix = "TheseLinesAudio",
 $outputRootDir = "c:\temp\"
) {
 Add-Type -AssemblyName System.Speech
 $reader = New-Object -TypeName System.IO.StreamReader($fileToSplit)
 $chunk = 1
 $speech = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
 $speech.SelectVoice("Microsoft Hazel Desktop")
 while (($line = $reader.ReadLine()) -ne $null) {
 if ($line -match $splitMarker) {
 } else {
 Add-Content -Path $(Get-FileName "txt") -Value $line -Encoding utf8
 $reader = $null
#entry point...