2

Using Powershell from Package Manager Console I can open and close file:

$DTE.ExecuteCommand(“File.OpenFile”, $file)
$DTE.ExecuteCommand(“File.Close”)

But when I try to save it:

$DTE.ExecuteCommand(“File.Save”, $file)

or

$DTE.ExecuteCommand(“File.Save”)

I get error:

PM> $DTE.ExecuteCommand(“File.Save”) Command "File.Save" is not valid. At line:1 char:1 + $DTE.ExecuteCommand(“File.Save”) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

How I can save it?

Actually I want to save it in other encoding:

enter image description here

Alexan
  • 8,165
  • 14
  • 74
  • 101

1 Answers1

2

You can save the file with

$DTE.ExecuteCommand("File.SaveSelectedItems")

There is also

$DTE.ExecuteCommand("File.SaveAll")

If you're doing this only to change the file's encoding however, I would suggest simply running the following PowerShell code that doesn't require $DTE and Visual Studio at all.

$content = Get-Content -Path $file -Encoding String
Set-Content -Value $content -Path $file -Encoding UTF8
dee-see
  • 23,668
  • 5
  • 58
  • 91
  • yes, it works. But how I can Save As... and choose encoding? I tried to use this PowerShell code (in your answer second part), but it didn't work for me, because I need UTF-8 without signature. But VS works fine if I do it manually. – Alexan Nov 12 '18 at 00:31
  • I coudln't find a way to open the encoding menu through `ExecuteCommand`, however for a PowerShell solution you can see this other SO question https://stackoverflow.com/q/5596982/446515 – dee-see Nov 12 '18 at 16:15
  • got list of commands by `$DTE.Commands File`, this command `File.SaveSelectedItemsAs` exists, but 'IsAvalable = false'. The same with 'File.AdvancedSaveOptions` – Alexan Nov 18 '18 at 19:29