<?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>Live PowerShell With Karl Prosser &#187; script</title>
	<atom:link href="http://karlprosser.com/coder/category/script/feed/" rel="self" type="application/rss+xml" />
	<link>http://karlprosser.com/coder</link>
	<description>invoke-intelligence &#124; where { $_.necessary }</description>
	<lastBuildDate>Fri, 22 Apr 2011 05:58:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Print-File Function</title>
		<link>http://karlprosser.com/coder/2009/11/10/print-file-function/</link>
		<comments>http://karlprosser.com/coder/2009/11/10/print-file-function/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 05:28:00 +0000</pubDate>
		<dc:creator>Karl</dc:creator>
				<category><![CDATA[PSV2]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://karlprosser.com/coder/2009/02/10/print-file-function/</guid>
		<description><![CDATA[Here is a simple function that will ask the associated program to print any file you pass in. It can work with a file as a parameter, or multiple files. How well this works, especially with multiple files at a time, depends on the associated application, first whether it supports the print verb, and secondly [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a simple function that will ask the associated program to print any file you pass in. It can work with a file as a parameter, or multiple files. How well this works, especially with multiple files at a time, depends on the associated application, first whether it supports the print verb, and secondly how it handles it – more importantly how it handles opening many files at once. I haven’t yet been brave enough to try it on PDFs with adobe reader.</p>
<pre><span style="color: #0000ff">function</span><span style="color: #000000"> </span><span style="color: #008080">print-file</span><span style="color: #000000">(</span><span style="color: #0000ff">$file</span><span style="color: #000000">)
{
 begin  {
    </span><span style="color: #0000ff">function</span><span style="color: #000000"> </span><span style="color: #008080">internal-printfile</span><span style="color: #000000">(</span><span style="color: #0000ff">$thefile</span><span style="color: #000000">)
    {
        </span><span style="color: #0000ff">if</span><span style="color: #000000"> (</span><span style="color: #0000ff">$thefile</span><span style="color: #000000"> </span><span style="color: #0000ff">-is</span><span style="color: #000000"> [</span><span style="color: #0000ff">string</span><span style="color: #000000">]) {</span><span style="color: #0000ff">$filename</span><span style="color: #000000"> </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">$thefile</span><span style="color: #000000"> }
        </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
                </span><span style="color: #0000ff">if</span><span style="color: #000000"> (</span><span style="color: #0000ff">$thefile</span><span style="color: #000000">.FullName </span><span style="color: #0000ff">-is</span><span style="color: #000000"> [</span><span style="color: #0000ff">string</span><span style="color: #000000">] ) { </span><span style="color: #0000ff">$filename</span><span style="color: #000000"> </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">$THEfile</span><span style="color: #000000">.FullName }
             }
        </span><span style="color: #0000ff">$start</span><span style="color: #000000"> </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #008080">new-object</span><span style="color: #000000"> System.Diagnostics.ProcessStartInfo </span><span style="color: #0000ff">$filename</span><span style="color: #000000">
        </span><span style="color: #0000ff">$start</span><span style="color: #000000">.Verb </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000080">"</span><span style="color: #000080">print</span><span style="color: #000080">"</span><span style="color: #000000">
        [System.Diagnostics.Process]::Start(</span><span style="color: #0000ff">$start</span><span style="color: #000000">)
    } 

</span><span style="color: #0000ff">if</span><span style="color: #000000"> (</span><span style="color: #0000ff">$file</span><span style="color: #000000"> </span><span style="color: #0000ff">-ne</span><span style="color: #000000"> </span><span style="color: #0000ff">$null</span><span style="color: #000000">) {
                </span><span style="color: #0000ff">$filespecified</span><span style="color: #000000"> </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">$true</span><span style="color: #000000">;
                </span><span style="color: #008080">internal-printfile</span><span style="color: #000000"> </span><span style="color: #0000ff">$file</span><span style="color: #000000">
            }
       }
process{</span><span style="color: #0000ff">if</span><span style="color: #000000"> (</span><span style="color: #000000">!</span><span style="color: #0000ff">$filespecified</span><span style="color: #000000">) { </span><span style="color: #008080">write-Host</span><span style="color: #000000"> process ; </span><span style="color: #008080">internal-printfile</span><span style="color: #000000"> </span><span style="color: #0000ff">$_</span><span style="color: #000000"> } } 

}</span></pre>
<p>@<a href="http://poshcode.org/843">poshcode</a> | <a href="http://poshcode.org/get/843">download</a></p>
<p>Here you can pass in a path to the file , or a fileobject that you get as a response from get-childitem (dir)</p>
<p>Here are a few examples</p>
<pre><span style="color: #008000">#</span><span style="color: #008000">look recursively through a folder and print all word documents</span><span style="color: #008000">
</span><span style="color: #000000">dir </span><span style="color: #000000">*</span><span style="color: #000000">.doc </span><span style="color: #000000">-</span><span style="color: #008080">r</span><span style="color: #000000"> </span><span style="color: #000000">|</span><span style="color: #000000"> </span><span style="color: #008080">print-file</span><span style="color: #000000"> 

</span><span style="color: #008000">#</span><span style="color: #008000">print one particular pdf file</span><span style="color: #008000">
</span><span style="color: #008080">print-file</span><span style="color: #000000"> c:\downloads\myfile.pdf</span></pre>
<p>Warning, this isn’t up to being a shrink wrapped deliverable. It doesn’t do any error handling, isn’t a nice V2 advanced function or anything, but that&#8217;s a beauty of PowerShell, you can just hack out something that fits your needs in a few minutes. With V2 parameter binding, this would be more elegant and actually simpler as i have to do some duplication of logic in my function to get it to work accepting the file both through the pipeline and as a parameter.</p>
<p>-Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://karlprosser.com/coder/2009/11/10/print-file-function/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

