Code formatting breaks when copying from Obsidian to PowerShell/other editors

Hi everyone,

I’m having an issue with code blocks in Obsidian not copying correctly to other applications (PowerShell ISE, cmd). When I copy PowerShell code from a fenced block (using ```powershell)

Example:
Original in notepad:

$desktopPath = [Environment]::GetFolderPath('Desktop')
$currentDate = Get-Date -Format "yyyy-MM-dd"
$outputPath = Join-Path -Path $desktopPath -ChildPath "g_$currentDate.csv"

Get-ChildItem -Path "F:\" -Recurse -File | 
Select-Object @{Name="FullPath"; Expression={$_.FullName}},
              @{Name="Drive"; Expression={$_.FullName.Split('\')[0]}},
              @{Name="Directory"; Expression={$_.DirectoryName}},
              @{Name="ParentFolder"; Expression={Split-Path $_.DirectoryName -Leaf}},
              @{Name="FileName"; Expression={$_.Name}},
              Length,
              @{Name="SizeMB"; Expression={[math]::Round($_.Length / 1MB, 2)}} | 
Export-Csv -Path $outputPath -NoTypeInformation -Encoding UTF8 -Delimiter "`t" -UseQuotes AsNeeded -Force

Write-Host "File saved: $outputPath" -ForegroundColor Green

Example:
Original in Obsidian:

```powershell

$desktopPath = [Environment]::GetFolderPath('Desktop')
$currentDate = Get-Date -Format "yyyy-MM-dd"
$outputPath = Join-Path -Path $desktopPath -ChildPath "g_$currentDate.csv"

Get-ChildItem -Path "F:\" -Recurse -File | 
Select-Object @{Name="FullPath"; Expression={$_.FullName}},
              @{Name="Drive"; Expression={$_.FullName.Split('\')[0]}},
              @{Name="Directory"; Expression={$_.DirectoryName}},
              @{Name="ParentFolder"; Expression={Split-Path $_.DirectoryName -Leaf}},
              @{Name="FileName"; Expression={$_.Name}},
              Length,
              @{Name="SizeMB"; Expression={[math]::Round($_.Length / 1MB, 2)}} | 
Export-Csv -Path $outputPath -NoTypeInformation -Encoding UTF8 -Delimiter "`t" -UseQuotes AsNeeded -Force

Write-Host "File saved: $outputPath" -ForegroundColor Green

```

Pasted result (in notepad):

$desktopPath = [Environment]::GetFolderPath('Desktop')
$currentDate = Get-Date -Format "yyyy-MM-dd"
$outputPath = Join-Path -Path $desktopPath -ChildPath "g_$currentDate.csv"

Get-ChildItem -Path "F:\" -Recurse -File | 
Select-Object @{Name="FullPath"; Expression={$_.FullName}},
              @{Name="Drive"; Expression={$_.FullName.Split('\')[0]}},
              @{Name="Directory"; Expression={$_.DirectoryName}},
              @{Name="ParentFolder"; Expression={Split-Path $_.DirectoryName -Leaf}},
              @{Name="FileName"; Expression={$_.Name}},
              Length,
              @{Name="SizeMB"; Expression={[math]::Round($_.Length / 1MB, 2)}} | 
Export-Csv -Path $outputPath -NoTypeInformation -Encoding UTF8 -Delimiter "`t" -UseQuotes AsNeeded -Force

Write-Host "File saved: $outputPath" -ForegroundColor Green

Has anyone encountered this? Are there plugin settings or workarounds that preserve exact formatting during copy/paste?

I edited your post to mark all the code as code blocks so we can see the exact text without the forum treating special characters as Markdown.

To put a code block in a code block, you can put a longer code fence around it like this (I’m using tildes, which are allowed in place of backticks, because they’re easier to type on mobile):

~~~~
~~~
code(
    in code
);
~~~
~~~~

I may be overlooking something because I’m on a phone which makes it harder to examine, but your examples all look the same to me. What is the difference you’re seeing?

1 Like

Yes, indeed all the examples look identical. Actually, the last example appear like this (see screenshot)

Here’s a copy-paste (from the code block button in Obsidian) into Notepad++:

into VS Code:

and into Notepad:


I’d check Notepad’s settings and/or try using a different code editor.

The problem is that the code is copied exactly as shown in the screenshot above—meaning if I paste it directly into PowerShell, it fails to execute.

Pasting into Notepad++ and WordPad works correctly (formatting is preserved).

However, this adds an extra step. I’d like the code to copy correctly by default without needing intermediate editors.

Is there a way to fix this in Obsidian’s settings or via a plugin? Ideally, copying from a ```powershell block should preserve the exact formatting for direct pasting into PowerShell.

If that’s a single line (in the sense of there being no newline characters in it), it may be a text encoding problem. This used to be an issue when opening text files in Notepad that were created on Linux or Mac, because the systems used different invisible control characters to represent new lines. (Notepad got an update in recent years that fixed that.)