Powershell script to print specific PDF pages into images
<p>How I could I change this powershell script</p>

<pre><code>Start-Process –FilePath “C:\Data\PROJECTS\ABC.pdf” –Verb Print -PassThru | %{sleep 10;$_} | kill
</code></pre>

<p>to:</p>

<ol>
<li>print specific pages of a PDF,   </li>
<li>directly to an image (e.g. png,jpg,tif, etc), and</li>
<li>save them accordingly?</li>
</ol>

<p>For example, I want to print pages 3,4,7 of ABC.pdf into three separate files called ABC_3.png, ABC_4.png, and ABC_7.png; the image file can be any format (.png, .jpg, .tif, etc.).</p>

<p>I plan to call on a .csv list to get all the parameter values (e.g.  page number to print, output name with page number, filepath to new file location, etc) but I don't know how to set up the powershell syntax. Thank you.</p>

<p><strong>UPDATE</strong>: </p>

<p>I've made progress on this task with the script below, which calls on a ghostcript. It does 1-3 above except that I can't seem to set my -dFirstPage and -dLastPage to a parameter from my csv... I get the powershell error:</p>

<pre><code>Invalid value for option -dFirstPage=$pg, use -sNAME = to define string constants
</code></pre>

<p>if I replace $pg with a number it seems to work fine. How would I use -sNAME to fix this?</p>

<p><strong>new SCRIPT</strong> </p>

<pre><code>#Path to your Ghostscript EXE
$tool = 'C:\Program Files\gs\gs9.19\bin\gswin64c.exe'

$files = Import-CSV 'C:\Data\files.csv' -Header ("FileName","Type","Map","Section","MapPg","SectionPg","Directory","PathName","LastWriteTime")

ForEach($File in $files) 
{

    if ($File.Map -eq "T" -And $File.Type -eq "pdf")
    {
        $tif = $File.PathName + "_Pg" + $File.MapPg + ".tif"
            $param = "-sOutputFile=$tif"
        $inputPDF = $File.PathName + ".pdf"
        $pg = $File.MapPg
        &amp; $tool -q -dNOPAUSE -sDEVICE=tiffg4 $param -r300 -dFirstPage='$pg' -dLastPage='$pg' $inputPDF -c quit
    }
    ElseIf ($File.Section -eq "T" -And $File.Type -eq "pdf") 
    {
        $tif = $File.PathName + $File.SectionPg + ".tif"
        $param = "-sOutputFile=$tif"
        $inputPDF = $File.PathName + ".pdf"
        $pg = $File.SectionPg
        &amp; $tool -q -dNOPAUSE -sDEVICE=tiffg4 $param -r300 -dFirstPage='$pg' -dLastPage='$pg' $inputPDF -c quit
    }
}
</code></pre>