<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: PowerShell script in a .bat file</title>
	<atom:link href="http://dmitrysotnikov.wordpress.com/2008/06/27/powershell-script-in-a-bat-file/feed/" rel="self" type="application/rss+xml" />
	<link>http://dmitrysotnikov.wordpress.com/2008/06/27/powershell-script-in-a-bat-file/</link>
	<description>Dmitry Sotnikov's view on PowerShell, PowerGUI and everything he sees around</description>
	<lastBuildDate>Mon, 30 Nov 2009 16:13:28 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Bruce Payette</title>
		<link>http://dmitrysotnikov.wordpress.com/2008/06/27/powershell-script-in-a-bat-file/#comment-4047</link>
		<dc:creator>Bruce Payette</dc:creator>
		<pubDate>Sun, 29 Jun 2008 00:27:05 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysotnikov.wordpress.com/?p=422#comment-4047</guid>
		<description>One way to completely avoid the quoting problem is to use the -encoded parameter on powershell.exe. This takes a Base64 encoded string, decodes it and then executes it. For example, we&#039;ll put some code in a script block (since it will be syntax checked) instead of a simple string:

PS (41) &gt; $code = {
&gt;&gt; #iterate numbers 1 through 10
&gt;&gt; 1..10 &#124; foreach-object {
&gt;&gt; # just output them
&gt;&gt; &quot;Current output:&quot;
&gt;&gt; $_
&gt;&gt; }
&gt;&gt;
&gt;&gt; }
&gt;&gt;

Now encode the command:

PS (42) &gt; $encoded = [convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($code))

The result of this looks like:

PS (43) &gt; $encoded
MQAuAC4AMQAwACAAfAAgAGYAbwByAGUAYQBjAGgALQBvAGIAagBlAGMAdAAgAHsACgAjACAAagB1AHMAdAAgAG8AdQB0AHAAdQB0ACAAdABoAGUAbQAKACI
AQwB1AHIAcgBlAG4AdAAgAG8AdQB0AHAAdQB0ADoAIgAKACQAXwAKAH0ACgA=

Then run it:

(CXT: win7) (STA-ISS) (44) &gt; powershell -encoded MQAuAC4AMQAwACAAfAAgAGYAbwByAGUAYQBjAGgALQBvAGIAagBlAGMAdAAgAHsACgAjACA
AagB1AHMAdAAgAG8AdQB0AHAAdQB0ACAAdABoAGUAbQAKACIAQwB1AHIAcgBlAG4AdAAgAG8AdQB0AHAAdQB0ADoAIgAKACQAXwAKAH0ACgA=

BTW - PowerShell has built-in support for this so when you call powershell from powershell with a scriptblock, the script block is automatically encoded and passed to the child process:

(CXT: win7) (STA-ISS) (46) &gt; powershell { &quot;Hi there. Today is &quot; + (get-date).DayOfWeek }
Hi there. Today is Saturday


-bruce
--------------------
Principal Developer, Windows PowerShell Team
Microsoft</description>
		<content:encoded><![CDATA[<p>One way to completely avoid the quoting problem is to use the -encoded parameter on powershell.exe. This takes a Base64 encoded string, decodes it and then executes it. For example, we&#8217;ll put some code in a script block (since it will be syntax checked) instead of a simple string:</p>
<p>PS (41) &gt; $code = {<br />
&gt;&gt; #iterate numbers 1 through 10<br />
&gt;&gt; 1..10 | foreach-object {<br />
&gt;&gt; # just output them<br />
&gt;&gt; &#8220;Current output:&#8221;<br />
&gt;&gt; $_<br />
&gt;&gt; }<br />
&gt;&gt;<br />
&gt;&gt; }<br />
&gt;&gt;</p>
<p>Now encode the command:</p>
<p>PS (42) &gt; $encoded = [convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($code))</p>
<p>The result of this looks like:</p>
<p>PS (43) &gt; $encoded<br />
MQAuAC4AMQAwACAAfAAgAGYAbwByAGUAYQBjAGgALQBvAGIAagBlAGMAdAAgAHsACgAjACAAagB1AHMAdAAgAG8AdQB0AHAAdQB0ACAAdABoAGUAbQAKACI<br />
AQwB1AHIAcgBlAG4AdAAgAG8AdQB0AHAAdQB0ADoAIgAKACQAXwAKAH0ACgA=</p>
<p>Then run it:</p>
<p>(CXT: win7) (STA-ISS) (44) &gt; powershell -encoded MQAuAC4AMQAwACAAfAAgAGYAbwByAGUAYQBjAGgALQBvAGIAagBlAGMAdAAgAHsACgAjACA<br />
AagB1AHMAdAAgAG8AdQB0AHAAdQB0ACAAdABoAGUAbQAKACIAQwB1AHIAcgBlAG4AdAAgAG8AdQB0AHAAdQB0ADoAIgAKACQAXwAKAH0ACgA=</p>
<p>BTW &#8211; PowerShell has built-in support for this so when you call powershell from powershell with a scriptblock, the script block is automatically encoded and passed to the child process:</p>
<p>(CXT: win7) (STA-ISS) (46) &gt; powershell { &#8220;Hi there. Today is &#8221; + (get-date).DayOfWeek }<br />
Hi there. Today is Saturday</p>
<p>-bruce<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
Principal Developer, Windows PowerShell Team<br />
Microsoft</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy</title>
		<link>http://dmitrysotnikov.wordpress.com/2008/06/27/powershell-script-in-a-bat-file/#comment-4045</link>
		<dc:creator>Andy</dc:creator>
		<pubDate>Sat, 28 Jun 2008 00:47:03 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysotnikov.wordpress.com/?p=422#comment-4045</guid>
		<description>powershell.exe -command &quot;1..10 &#124; foreach-object { &#039;Current output:&#039;; $_; }&quot;</description>
		<content:encoded><![CDATA[<p>powershell.exe -command &#8220;1..10 | foreach-object { &#8216;Current output:&#8217;; $_; }&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stuart</title>
		<link>http://dmitrysotnikov.wordpress.com/2008/06/27/powershell-script-in-a-bat-file/#comment-4043</link>
		<dc:creator>Stuart</dc:creator>
		<pubDate>Fri, 27 Jun 2008 11:59:30 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysotnikov.wordpress.com/?p=422#comment-4043</guid>
		<description>As far as I can see, the double quotes will get the command interpreter confused, so you will have to escape them - as in 

powershell.exe -command &quot;1..10 &#124; foreach-object { \&quot;Current ou
tput:\&quot;; $_; }&quot;

If you don&#039;t escape them you get the message:

The term &#039;Current&#039; is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At line:1 char:33
+ 1..10 &#124; foreach-object { Current  &lt;&lt;&lt;&lt; output:; $_; }

You also missed off the closing double quote, but that didn&#039;t seem to make a difference.

Thanks for the post though, it could be useful for short scripts.</description>
		<content:encoded><![CDATA[<p>As far as I can see, the double quotes will get the command interpreter confused, so you will have to escape them &#8211; as in </p>
<p>powershell.exe -command &#8220;1..10 | foreach-object { \&#8221;Current ou<br />
tput:\&#8221;; $_; }&#8221;</p>
<p>If you don&#8217;t escape them you get the message:</p>
<p>The term &#8216;Current&#8217; is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.<br />
At line:1 char:33<br />
+ 1..10 | foreach-object { Current  &lt;&lt;&lt;&lt; output:; $_; }</p>
<p>You also missed off the closing double quote, but that didn&#8217;t seem to make a difference.</p>
<p>Thanks for the post though, it could be useful for short scripts.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
