use PowerShell to save file in Visual Studio









up vote
1
down vote

favorite












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










share|improve this question























  • See stackoverflow.com/questions/5406172/utf-8-without-bom
    – Sergey Vlasov
    Nov 12 at 5:47










  • @SergeyVlasov, yes I do it manually in VS, I just want to automate this
    – Alexan
    Nov 12 at 12:31






  • 1




    The answers from the link above mention Fix File Encoding and editorconfig that remove the need to do it manually.
    – Sergey Vlasov
    Nov 12 at 17:18











  • @SergeyVlasov, I don't see in menu "Advanced save options"
    – Alexan
    Nov 12 at 17:42










  • Advanced Save Options missing in Visual Studio 2017
    – Alexan
    Nov 12 at 17:46














up vote
1
down vote

favorite












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










share|improve this question























  • See stackoverflow.com/questions/5406172/utf-8-without-bom
    – Sergey Vlasov
    Nov 12 at 5:47










  • @SergeyVlasov, yes I do it manually in VS, I just want to automate this
    – Alexan
    Nov 12 at 12:31






  • 1




    The answers from the link above mention Fix File Encoding and editorconfig that remove the need to do it manually.
    – Sergey Vlasov
    Nov 12 at 17:18











  • @SergeyVlasov, I don't see in menu "Advanced save options"
    – Alexan
    Nov 12 at 17:42










  • Advanced Save Options missing in Visual Studio 2017
    – Alexan
    Nov 12 at 17:46












up vote
1
down vote

favorite









up vote
1
down vote

favorite











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










share|improve this question















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







visual-studio powershell automation character-encoding






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 3:10

























asked Nov 11 at 2:45









Alexan

4,15774874




4,15774874











  • See stackoverflow.com/questions/5406172/utf-8-without-bom
    – Sergey Vlasov
    Nov 12 at 5:47










  • @SergeyVlasov, yes I do it manually in VS, I just want to automate this
    – Alexan
    Nov 12 at 12:31






  • 1




    The answers from the link above mention Fix File Encoding and editorconfig that remove the need to do it manually.
    – Sergey Vlasov
    Nov 12 at 17:18











  • @SergeyVlasov, I don't see in menu "Advanced save options"
    – Alexan
    Nov 12 at 17:42










  • Advanced Save Options missing in Visual Studio 2017
    – Alexan
    Nov 12 at 17:46
















  • See stackoverflow.com/questions/5406172/utf-8-without-bom
    – Sergey Vlasov
    Nov 12 at 5:47










  • @SergeyVlasov, yes I do it manually in VS, I just want to automate this
    – Alexan
    Nov 12 at 12:31






  • 1




    The answers from the link above mention Fix File Encoding and editorconfig that remove the need to do it manually.
    – Sergey Vlasov
    Nov 12 at 17:18











  • @SergeyVlasov, I don't see in menu "Advanced save options"
    – Alexan
    Nov 12 at 17:42










  • Advanced Save Options missing in Visual Studio 2017
    – Alexan
    Nov 12 at 17:46















See stackoverflow.com/questions/5406172/utf-8-without-bom
– Sergey Vlasov
Nov 12 at 5:47




See stackoverflow.com/questions/5406172/utf-8-without-bom
– Sergey Vlasov
Nov 12 at 5:47












@SergeyVlasov, yes I do it manually in VS, I just want to automate this
– Alexan
Nov 12 at 12:31




@SergeyVlasov, yes I do it manually in VS, I just want to automate this
– Alexan
Nov 12 at 12:31




1




1




The answers from the link above mention Fix File Encoding and editorconfig that remove the need to do it manually.
– Sergey Vlasov
Nov 12 at 17:18





The answers from the link above mention Fix File Encoding and editorconfig that remove the need to do it manually.
– Sergey Vlasov
Nov 12 at 17:18













@SergeyVlasov, I don't see in menu "Advanced save options"
– Alexan
Nov 12 at 17:42




@SergeyVlasov, I don't see in menu "Advanced save options"
– Alexan
Nov 12 at 17:42












Advanced Save Options missing in Visual Studio 2017
– Alexan
Nov 12 at 17:46




Advanced Save Options missing in Visual Studio 2017
– Alexan
Nov 12 at 17:46












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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





share|improve this answer






















  • 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 at 0: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 stackoverflow.com/q/5596982/446515
    – dee-see
    Nov 12 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 at 19:29










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245405%2fuse-powershell-to-save-file-in-visual-studio%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










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





share|improve this answer






















  • 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 at 0: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 stackoverflow.com/q/5596982/446515
    – dee-see
    Nov 12 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 at 19:29














up vote
1
down vote



accepted










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





share|improve this answer






















  • 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 at 0: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 stackoverflow.com/q/5596982/446515
    – dee-see
    Nov 12 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 at 19:29












up vote
1
down vote



accepted







up vote
1
down vote



accepted






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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 16:12

























answered Nov 12 at 0:12









dee-see

18.7k34479




18.7k34479











  • 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 at 0: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 stackoverflow.com/q/5596982/446515
    – dee-see
    Nov 12 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 at 19:29
















  • 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 at 0: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 stackoverflow.com/q/5596982/446515
    – dee-see
    Nov 12 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 at 19:29















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 at 0:31





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 at 0: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 stackoverflow.com/q/5596982/446515
– dee-see
Nov 12 at 16:15




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
– dee-see
Nov 12 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 at 19:29




got list of commands by $DTE.Commands File, this command File.SaveSelectedItemsAs exists, but 'IsAvalable = false'. The same with 'File.AdvancedSaveOptions`
– Alexan
Nov 18 at 19:29

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245405%2fuse-powershell-to-save-file-in-visual-studio%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3