Import notes and tidy up aliases with powershell

Hey there, I’ve been importing and cleaning up some notes from google keep, and a lot of them came over with aliases.

I wanted to remove these with powershell, but I’ve been running into some problems, here’s the script I wrote:

# Define the pattern to match files - "*.md" for Markdown files
$filePattern = "*.md"

# Iterate over all .md files in the directory and its subdirectories
Get-ChildItem -Path . -Filter $filePattern -Recurse | ForEach-Object {
    $filePath = $_.FullName

    # Read the content of the file
    $fileContent = Get-Content -Path $filePath -Raw

    # Define a regex pattern to match the 'aliases' property and any multiline content until the next property or end of the file
    $pattern = '(?ms)\naliases:.*?(?=\r?\n\S|\Z)'

    # Check if the file content matches the pattern
    if ($fileContent -match $pattern) {
        # Replace the 'aliases' property and its content with nothing
        $newContent = $fileContent -replace $pattern, ''

        # Write the modified content back to the file ONLY if there was a match
        # Using [System.IO.File]::WriteAllText to ensure UTF-8 encoding without BOM
        $utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $false
        [System.IO.File]::WriteAllText($filePath, $newContent, $utf8NoBomEncoding)
    }
}

Write-Host "Files containing 'aliases' have been processed."

I was testing out a few format options in powershell because I noticed two things

  1. obsidian was not recognizing the yaml properties immedietely after the running this script, if I modified the file in the obsidian editor then the properties would return.
  2. when I tried to backup with git, the diff shows that each line in the file has changed, ie.
----
-aliases:
-tags:
-  - questions
-  - dynamo
----
-Is refinery an evolutionary solver?
-
-Van wijnen project
+---
+tags:
+  - questions
+  - dynamo
+---
+Is refinery an evolutionary solver?
+
+Van wijnen project

Whether I used no explicit encoding, UTF8, or UTF8NoBomEncoding, I get these two persistent problems. Does anyone have any idea how to clean this up, or if I’m even on the right track here?

Cheers
Dan

I’ve tried this in both Powershell 5.1 and 7.4.1 with no success

It could be that you’ve got some line ending issue since git is complaining like that. Check that your files actually ends with just \n and not the “older” \r\n windows combination.

1 Like

GitHub Desktop prints alerts as well on notes when these things happen.

Comment out the line that writes back to the file and instead display the output on the screen, then run against a couple files you know need fixing to see if you get the expected results. FWIW, I use Get-Content -Encoding UTF8 and Set-Content -Encoding UTF8 when working with my files, and haven’t seen any problems. @holroy brings up a good point, most of my files have \r\n. Early on I ran into problems with that and used Notepad++ to go through all my files and replace all variations with \r\n so things were consistent (you could easily also use PowerShell or other tools). You could of course also modify your scripts to accommodate various line endings.