<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Huddled Masses &#187; Clipboard</title>
	<atom:link href="http://huddledmasses.org/tag/clipboard/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Clip.exe and the missing Paste.exe</title>
		<link>http://huddledmasses.org/clipexe-and-the-missing-pasteexe/</link>
		<comments>http://huddledmasses.org/clipexe-and-the-missing-pasteexe/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 16:13:23 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Clipboard]]></category>
		<category><![CDATA[Command Line]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Threading]]></category>
		<category><![CDATA[Utilities]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=783</guid>
		<description><![CDATA[One of the guys on #PowerShell in IRC was asking about how to get the contents of the clipboard in PowerShell, and after we had tried several different scripts, we realized that in Version 1, because PowerShell runs in an MTA thread, you cannot access the clipboard without creating a new thread (in STA mode). [...]]]></description>
			<content:encoded><![CDATA[	<p>One of the guys on <a href="irc://irc.freenode.net/PowerShell">#PowerShell in IRC</a> was asking about how to <strong>get</strong> the contents of the clipboard in PowerShell, and after we had tried several different scripts, we realized that in Version 1, because PowerShell runs in an <a href="http://support.microsoft.com/kb/150777"><span class="caps">MTA</span> thread</a>, you cannot access the clipboard without creating a new thread (in <span class="caps">STA</span> mode).  After I got done ranting, and pointing out that everyone needs to use PowerShell v2 (yeah, it&#8217;s still in <span class="caps">CTP</span>, so I was mostly kidding) in <a href="http://support.microsoft.com/kb/150777">-STA</a> mode &#8230; I whipped together the C# one liner and stuck it into a class with instructions for compiling, and figured I might as well share.</p>

	<p>The code is <a href="clipexe-and-the-missing-pasteexe#more-783">below</a>. There are two files: Clip.cs and Paste.cs &#8230; you probably only need Paste.cs, because Clip.exe is <a href="http://weblogs.asp.net/owscott/archive/2006/12/15/clip-saving-command-line-and-powershell-output-directly-to-the-clipboard.aspx">already included</a> in Windows <em>after</em> Windows XP, but for the sake of completeness (and because it was just a few lines), I wrote them both.</p>

	<p>To use them, you must compile them using <code>Csc.exe</code>, which is included as part of the .Net Framework.  If it&#8217;s not already on your path, it&#8217;s in the folder for your most recent .Net Framework version (eg: C:\WINDOWS\Microsoft.NET\Framework\v3.5\csc.exe  or C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe). All you have to do is run: <code>csc Paste.cs</code> and <code>csc Clip.cs</code> and you should end up with a <code>Paste.exe</code> and <code>Clip.exe</code> which you can use to copy and paste from the command-line&#8230; <span id="more-783"></span></p>

	<h3>Paste.cs</h3>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Windows.Forms</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF;">namespace</span> Huddled <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Paste <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#91;</span>STAThread<span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args <span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> line <span style="color: #0600FF;">in</span> Clipboard.<span style="color: #0000FF;">GetText</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span>,<span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StringSplitOptions.<span style="color: #0000FF;">None</span> <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span> line <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div>

	<h3>Clip.cs</h3>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Windows.Forms</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF;">namespace</span> Huddled <span style="color: #000000;">&#123;</span><br />
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Clip <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#91;</span>STAThread<span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args <span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">string</span> s<span style="color: #008000;">;</span> <br />
&nbsp; &nbsp; &nbsp; StringBuilder output <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StringBuilder<span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Join</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot; &quot;</span>, args<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>s <span style="color: #008000;">=</span> Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;output.<span style="color: #0000FF;">AppendLine</span><span style="color: #000000;">&#40;</span>s<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; Clipboard.<span style="color: #0000FF;">SetText</span><span style="color: #000000;">&#40;</span> output.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/clipexe-and-the-missing-pasteexe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating PowerShell Scripts from your Command History</title>
		<link>http://huddledmasses.org/creating-powershell-scripts-from-your-command-history/</link>
		<comments>http://huddledmasses.org/creating-powershell-scripts-from-your-command-history/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 18:57:31 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Clipboard]]></category>
		<category><![CDATA[Get-History]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/creating-powershell-scripts-from-your-command-history/</guid>
		<description><![CDATA[The other day I was working on some scripts, trying to figure out how to do something new with WMI, and when I finally got it working, I started to copy and paste the commands I had typed into a script so that I could repeat the action in the future &#8230; and I realized [...]]]></description>
			<content:encoded><![CDATA[	<p>The other day I was working on some scripts, trying to figure out how to do something new with <span class="caps">WMI</span>, and when I finally got it working, I started to copy and paste the commands I had typed into a script so that I could repeat the action in the future &#8230; and I realized that I could just use the <code>Get-History</code> cmdlet and export the <code>CommandLine</code> property of the history items to the clipboard! Wow, that&#8217;s such a timesaver &#8230;</p>

	<p>Of course, once I thought of it, I figured that really, I should just write save them straight to a script, where I could delete the ones I didn&#8217;t want.  Of course, I&#8217;m lazy, so I don&#8217;t want to type any more than I have to, and I figured this was worth turning into a script of it&#8217;s own!  I&#8217;ve polished it a bit now, so that it automatically creates a script in my &#8220;Scripts&#8221; folder (which is included in my path) ... and in fact, I even went ahead and tweaked my <code>Edit-File</code> script (an idea I got from the <span class="caps">PSCX</span> guys) so that it accepts files from the pipeline so that I can write: <code>save Test-Script 20 | edit</code> to open the file right away.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## New-Script function</span><br />
<span style="color: #666666; font-style: italic;">## Creates a new script from the most recent commands in history</span><br />
<span style="color: #666666; font-style: italic;">##################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## Example Usage:</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp;New-Script ScriptName 4</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp; &nbsp;creates a script from the most recent four commands </span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp;New-Script Clipboard -id 10,11,12,14</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp; &nbsp;sends the the specified commands from the history to the clipboard</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp;Notepad (New-Script ScriptName 20)</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp; &nbsp;sends the most recent twenty commands to the script, and then opens the script in notepad</span><br />
<span style="color: #666666; font-style: italic;">##################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## As a tip, I use a prompt function something like this to get the ID into the prompt:</span><br />
<span style="color: #666666; font-style: italic;">##</span><br />
<span style="color: #666666; font-style: italic;">## function prompt {</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; return &quot;`[{0}]: &quot; -f ((get-history -count 1).Id + 1)</span><br />
<span style="color: #666666; font-style: italic;">## }</span><br />
<span style="color: #666666; font-style: italic;">##################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## Revision History</span><br />
<span style="color: #666666; font-style: italic;">## 1.0 - initial release</span><br />
<span style="color: #666666; font-style: italic;">## 1.1 - fix bug with specifying multiple IDs</span><br />
<span style="color: #666666; font-style: italic;">## 2.0 - use the current folder as the default instead of throwing an exception</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; - prompt to overwrite if not -Force</span><br />
<span style="color: #666666; font-style: italic;">##################################################################################################</span><br />
<br />
<span style="color: #666666; font-style: italic;">#function New-Script {</span><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$script</span><span style="color: #66cc66;">=</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Read-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;A name for your script&quot;</span><span style="color: #333;">&#41;</span>,<br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$count</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>, <br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$id</span><span style="color: #66cc66;">=</span>@<span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">History</span></span> <span style="color: #000066;">-count</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> Id<span style="color: #333;">&#41;</span>.<span style="color: #003366;">Id</span><span style="color: #333;">&#41;</span>,<br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Force</span><br />
<span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># if there's only one id, then the count counts, otherwise we just use the ids</span><br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$id</span>.<span style="color: #003366;">Count</span> <span style="color: #000066;">-eq</span> <span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> 1..<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$count</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span><span style="color: #66cc66;">|%</span><span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$id</span> <span style="color: #66cc66;">+=</span> <span style="color: #660033; font-weight: bold;">$id</span><span style="color: #333;">&#91;</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #333;">&#125;</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #666666; font-style: italic;"># Get the CommandLines from the history items...</span><br />
<span style="color: #660033; font-weight: bold;">$commands</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">History</span></span> <span style="color: #000066;">-id</span> <span style="color: #660033; font-weight: bold;">$id</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">&amp;</span>amp;amp;amp;<span style="color: #333;">&#123;</span><span style="color: #666699; font-weight: bold;">process</span><span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">CommandLine</span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$script</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;clipboard&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> @<span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">PSSnapin</span></span> <span style="color: #000066;">-Name</span> <span style="color: #009900;">&quot;pscx&quot;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Count</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$commands</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">out-<span style="font-style: normal;">clipboard</span></span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">elseif</span><span style="color: #333;">&#40;</span>@<span style="color: #333;">&#40;</span><span style="color: #660033;">gcm</span> clip.<span style="color: #003366;">exe</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Count</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$commands</span> <span style="color: #66cc66;">|</span> clip<br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$folder</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Split-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$script</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$folder</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># default to putting it in my &quot;Windows PowerShell\scripts&quot; folder which I have in my path...</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$folder</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Join-<span style="font-style: normal;">Path</span></span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Split-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$Profile</span><span style="color: #333;">&#41;</span> <span style="color: #009900;">&quot;Scripts&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$folder</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># if that fails, put it in the current path (on the file system)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$folder</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Location</span></span> <span style="color: #000066;">-PSProvider</span> <span style="color: #009900;">&quot;FileSystem&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># add the ps1 extension if it's not already there ...</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$file</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Join-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$folder</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Split-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$script</span> <span style="color: #000066;">-leaf</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>IO.<span style="color: #003366;">FileInfo</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$file</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Extension</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$file</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;$file.ps1&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># write an error message if the file already exists, unless -Force</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$file</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-and</span> <span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$Force</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Error</span></span> <span style="color: #009900;">&quot;The file already exists, do you want to overwrite?&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># and confirm before setting the content if the file already exists, unless -Force</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$commands</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">set-<span style="font-style: normal;">content</span></span> <span style="color: #660033; font-weight: bold;">$file</span> <span style="color: #000066;">-Confirm</span>:<span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$file</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-and</span> <span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$Force</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Item</span></span> <span style="color: #660033; font-weight: bold;">$file</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666666; font-style: italic;">#}</span></div>

	<p>There&#8217;s way more script that is strictly needed here, but it&#8217;s mostly because I wanted to make typing the script extension and path optional, since I put almost all of my scripts in the same scripts folder, and the extension is always the same.</p>

	<p>As you can see, you can choose to just grab the most recent <code>X</code> commands, or you can choose specific commands by id that you want to capture in the script. You can also output them to the clipboard, as long as you either have the <span class="caps">PSCX</span> snapin with <code>out-clipboard</code> loaded, or you have clip.exe (which is not included on XP).</p>

	<p>This version of this script is in <a href="http://powershellcentral.com/scripts/171">the PowerShell repository</a> and if you make any improvements, you can submit them through the <em>correction or amendment</em> form &#8230;</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/creating-powershell-scripts-from-your-command-history/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

