63

I am trying to run a powershell -Command to execute a powershell file as a post build event in Visual studio 2013 But i am getting

'Powershell' is not recognized as an internal or external command, operable program or batch file

error on output window and

Powershell -Command exited with code 9009 error

Full Error Message:

'Powershell' is not recognized as an internal or external command,
10>  operable program or batch file.
10>C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets(4429,5): error MSB3073: The command "echo "C:\dev\tfs\Main\Shared\AggregationComponents\GenerateSchema.cmd"
10>C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets(4429,5): error MSB3073: call "C:\dev\tfs\Main\Shared\AggregationComponents\GenerateSchema.cmd"
10>C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets(4429,5): error MSB3073: echo "Move the PhoenixData namespace schema suffixing it"
10>C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets(4429,5): error MSB3073: Powershell -Command "C:\dev\tfs\Main\Shared\AggregationComponents\MoveAndRenameXsds.ps1 'C:\dev\tfs\Main\Shared\AggregationComponents\bin\Debug\' 'C:\dev\tfs\Main\Shared\AggregationComponents\..\PublishedAnalyticsXsds' '.Aggregation'"" exited with code 9009.
========== Rebuild All: 8 succeeded, 1 failed, 1 skipped ==========
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Rahul Lodha
  • 3,601
  • 7
  • 25
  • 34
  • The unmatched quotation marks are suspicious. Are you sure you copied it correctly? Please share the actual command line you are trying to execute. (Also verify that it is on your PATH.) – Raymond Chen Apr 21 '15 at 16:58
  • @RahulLodha This problem could be due to pathext variable problem. See http://superuser.com/questions/675006/ – X10 Sep 18 '15 at 02:54
  • An old thread I know, but I had this issue recently using the JetBrains WebStorm CLI (similar to VS Code) and turns out that if I used Windows Command Prompt as Administrator it worked fine. – James Dec 14 '21 at 23:08

5 Answers5

102

Sounds like you're missing an environment variable.

Add this to your Path environment variable:

%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\

OR

Change your script to this:

%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe -Command "your command"
Jon Tirjan
  • 3,556
  • 2
  • 16
  • 24
  • 4
    It worked. i had to restart visual studio after path change. Thank You! :) – Rahul Lodha Apr 21 '15 at 17:30
  • 2
    Glad it worked. Strange that it was missing though. I've always seen it populated by default. – Jon Tirjan Apr 21 '15 at 17:31
  • What worked for me was C:\WINDOWS\System32\WindowsPowerShell\v1.0\ – Zeyad Shaban Apr 27 '21 at 10:31
  • I had to move the path %SYSTEMROOT%\System32\WindowsPowerShell\v1.0\ from the System environment list box to the User environment list box, as per this answer: https://superuser.com/a/1034020 . Otherwise the %SYSTEMROOT% part was not expanding to actual path. – qaziqarta Aug 04 '22 at 05:56
11

You only need to add it to your environment variables, just follow the following two simple steps:

1.write env in the search bar

or Right click start button -> System -> Advanced system settings -> Environment Variables... -> Select PATH

2. click on the new button and add the following path:

C:\WINDOWS\System32\WindowsPowerShell\v1.0\

or %SYSTEMROOT%\System32\WindowsPowerShell\v1.0\


write env in the search bar

choose path

click Ok

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mostafa Wael
  • 2,750
  • 1
  • 21
  • 23
4

Remove BOM byte from the beginning of batch file or convert it's encoding to ANSI. BOM treats system to think you use unavailable command.

alloha
  • 133
  • 1
  • 13
2

My mistake was just that I forgot to run Visual Studio / command prompt as an Administrator.

Alex
  • 5,364
  • 9
  • 54
  • 69
0

try

'%SYSTEMROOT%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe tracert google.com'

might require \\ instead of \.

Arif Test
  • 3
  • 3