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

6
  • See stackoverflow.com/questions/5406172/utf-8-without-bom Commented Nov 12, 2018 at 5:47
  • @SergeyVlasov, yes I do it manually in VS, I just want to automate this Commented Nov 12, 2018 at 12:31
  • 1
    The answers from the link above mention Fix File Encoding and editorconfig that remove the need to do it manually. Commented Nov 12, 2018 at 17:18
  • @SergeyVlasov, I don't see in menu "Advanced save options" Commented Nov 12, 2018 at 17:42
  • Advanced Save Options missing in Visual Studio 2017 Commented Nov 12, 2018 at 17:46

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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 stackoverflow.com/q/5596982/446515
got list of commands by $DTE.Commands File, this command File.SaveSelectedItemsAs exists, but 'IsAvalable = false'. The same with 'File.AdvancedSaveOptions`

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.