Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two strings using Visual Studio Code?

Visual Studio Code can be used to compare files.

"%LOCALAPPDATA%\Programs\Microsoft VS Code\code.exe" --diff file1.cs file2.cs

However, is it possible to use it to compare two texts stored in PowerShell variables?

$s1 = "abc
cde"
$s2 = "abc
xyz"
& "%LOCALAPPDATA%\Programs\Microsoft VS Code\code.exe" --diff ....?

The Compare-Object cmdlet just show two pieces of texts if they are different. It's really useless when the texts are big.

like image 483
ca9163d9 Avatar asked May 28 '26 07:05

ca9163d9


1 Answers

VSCode will only diff files, so save your buffers to files.

$s1 = "abc
cde"
$s2 = "abc
xyz"

$f1 = New-TemporaryFile
$f2 = New-TemporaryFile

$s1 | Out-File $f1.FullName
$s2 | Out-File $f2.FullName

& "C:\Program Files\Microsoft VS Code\Code.exe" --diff $f1.FullName $f2.FullName

Read-Host -Prompt "Hit ENTER after you have compared temp files, and they will be deleted"

Write-Host "Removing $($f1.FullName) and $($f2.FullName)"

Remove-Item $f1.FullName
Remove-Item $f2.FullName
like image 197
Kory Gill Avatar answered May 31 '26 23:05

Kory Gill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!