<?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; Get-History</title>
	<atom:link href="http://huddledmasses.org/tag/get-history/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>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>

