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:
visual-studio powershell automation character-encoding
|
show 1 more comment
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:
visual-studio powershell automation character-encoding
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
|
show 1 more comment
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:
visual-studio powershell automation character-encoding
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:
visual-studio powershell automation character-encoding
visual-studio powershell automation character-encoding
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
|
show 1 more comment
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
|
show 1 more comment
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
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 throughExecuteCommand
, 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 commandFile.SaveSelectedItemsAs
exists, but 'IsAvalable = false'. The same with 'File.AdvancedSaveOptions`
– Alexan
Nov 18 at 19:29
add a comment |
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
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 throughExecuteCommand
, 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 commandFile.SaveSelectedItemsAs
exists, but 'IsAvalable = false'. The same with 'File.AdvancedSaveOptions`
– Alexan
Nov 18 at 19:29
add a comment |
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
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 throughExecuteCommand
, 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 commandFile.SaveSelectedItemsAs
exists, but 'IsAvalable = false'. The same with 'File.AdvancedSaveOptions`
– Alexan
Nov 18 at 19:29
add a comment |
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
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
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 throughExecuteCommand
, 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 commandFile.SaveSelectedItemsAs
exists, but 'IsAvalable = false'. The same with 'File.AdvancedSaveOptions`
– Alexan
Nov 18 at 19:29
add a comment |
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 throughExecuteCommand
, 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 commandFile.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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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