3

This question already has an answer here:

From How to run a PowerShell script within a Windows batch file

There's an idiom in CMD scripts to use :: as a comment delimiter. (: means a label, so :: gets ignored.) I use it because I think it's easier to read than REM.

I wrote this in an answer:

@@:: This prolog allows a PowerShell script to be embedded in a .CMD file.
@@:: Any non-PowerShell content must be preceeded by "@@"
@@setlocal
@@set POWERSHELL_BAT_ARGS=%*
@@if defined POWERSHELL_BAT_ARGS set POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%
@@PowerShell -Command Invoke-Expression $('$args=@(^&{$args} %POWERSHELL_BAT_ARGS%);'+[String]::Join(';',$((Get-Content '%~f0') -notmatch '^^@@'))) & goto :EOF

which formatted like this (as of 9 April 2010; red circle added): alt text
(source: ggpht.com)

marked as duplicate by Manishearth, hims056, ben is uǝq backwards, Tobias Kienzler, Danny Beckett Jul 3 '13 at 12:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

the SO syntax highlighter is not a magical language detector. It uses simple rules for colorization that happens to work decently with most languages. PowerShell is apparently not one of them.

  • Yeah, I get that SO's syntax highlighter is general-purpose that way. Note that the issue here is with CMD, not with PowerShell. (When I read what you wrote, I think you are telling me I shouldn't have reported this issue. Do you think the SO team doesn't want to know about it?) – Jay Bazuzi Apr 11 '10 at 18:44
  • @Jay, I think this will be closed as a will-not-fix or by-design bug. The SO team has better things to do than change it so it works with CMD or Powershell or any other language perfectly. Plus, any changes to the highlighting engine at this point would cause old posts to highlight differently, possibly for the worse. If you want perfect syntax highlighting, put it on pastebin.com or similar. – Earlz Apr 12 '10 at 1:59
  • 1
    will-not-fix is fine; the StackOverflow team gets to decide where to put their energy. by-design seems nuts: who would say that this behavior is desirable? As for back-compat, it would be possible to teach the formatter to apply old rules to posts before a certain date, and new rules to newer posts. Again, up to the SO team to decide what they want to do. – Jay Bazuzi Apr 14 '10 at 18:00
  • @Jay that just adds a lot of complicated implementation details for very little real benefit. If you want that, go cast your vote for that feature-request for a language selector for code formatting – Earlz Apr 14 '10 at 21:31
3

You can do something similar to what the VB crowd does:

@@::// This prolog allows a PowerShell script to be embedded in a .CMD file.
@@::// Any non-PowerShell content must be preceeded by "@@"
@@setlocal
@@set POWERSHELL_BAT_ARGS=%*
@@if defined POWERSHELL_BAT_ARGS set POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%
@@PowerShell -Command Invoke-Expression $('$args=@(^&{$args} %POWERSHELL_BAT_ARGS%);'+[String]::Join(';',$((Get-Content '%~f0') -notmatch '^^@@'))) & goto :EOF

@@::# will also work.

Not a great solution, but you can render the code properly.

  • Thanks, that's a good tip to know. This is an excellent example of "Each design pattern is a sign of a weakness in the programming language to which it applies". – Jay Bazuzi Apr 14 '10 at 18:02

Not the answer you're looking for? Browse other questions tagged .