PowerShell
top of page

This night's post shows how you can use a PowerShell script to save each page of an online book as a PDF.


The process involves using an add-in for Chrome called Screenshot Capture.   See: https://chromewebstore.google.com/detail/screenshot-capture/giabbpobpebjfegnpcclkocepcgockkc

 

When this is installed, when you press ALT + S it will automatically save a .png file of the open page to a specified location.  Use these settings, with the option for ‘Capture Viewport’.

  


  


This is the script:

 

Add-Type -AssemblyName System.Windows.Forms

 

$pathToChrome = 'C:\Program Files\Google\Chrome\Application\chrome.exe'

$tempFolder = '--user-data-dir=c:\temp' # pick a temp folder for user data

$startmode = '--start-fullscreen' # '--kiosk' is another option

 

Start-Process -FilePath $pathToChrome -ArgumentList $tempFolder, $startmode, $startPage

 

Start-Sleep -Seconds 5 # Wait for the browser to load

 

for ($i = 0; $i -lt 10; $i++) {

 

    # Press Alt+S

    [System.Windows.Forms.SendKeys]::SendWait('%s')

    Start-Sleep -Milliseconds 5000 # Small delay between key presses

   

    # Press Page Down

    [System.Windows.Forms.SendKeys]::SendWait('{PGDN}')

    Start-Sleep -Milliseconds 5000 # Small delay between key presses

    }

 

 

 Be sure to confirm that the path on the second line is where you have Chrome installed on your PC.


Enter the url of the web page on the line beginning, ‘$startPage’

 

Indicate the number of pages you need to grab here [in this example it’s 10 pages]

 for ($i = 0; $i -lt 10; $i++) {

 

The PowerShell script in effect presses ‘ALT + S’ and then the page down key for you.

 

It’s possible that the script can get thrown off if a page takes a long time to load, so the last part of the script pauses for a set amount of milliseconds.   So 5000 equals 5 seconds.


In Chrome, be sure to enter full screen mode by pressing F11.

 

Just enter the script in the white pane at the top, and then press the play button.



After the script ends and the .png files are generated, you can easily convert them to a single PDF in Adobe Acrobat.


You can use a simple PowerShell script to extract the contents of multiple zip files. The command Expand-Archive followed by a file path to a zip file, will extract the contents to a location specified in the script.


Create a text file with each line beginning with Expand-Archive -LiteralPath '


. . . followed by the file paths, followed by  -DestinationPath 

. . . followed by the location that you want the files to be extracted to:


Expand-Archive -LiteralPath "C:\foofolder\Test.zip" -DestinationPath "C:\foofolder\extracthere"

Expand-Archive -LiteralPath "C:\foofolder\Litigation Support.7z" -DestinationPath "C:\foofolder\extracthere"


Open Windows PowerShell ISE (x86), and paste this script into the script pane.



Note that this method will not work for 7zip files.



I tested this tonight and successfully extracted more than 50,000 files from almost 50 zip files.



Updated: Apr 15, 2023

The Tip of the Night for December 24, 2022 noted that using a PowerShell script to copy multiple files from multiple locations to another location will work faster than XCOPY.


There is some debate online about whether or not the robocopy command will be faster than Windows Explorer copying the contents of one directory to another folder. I decided to test Explorer, robocopy, xcopy, and PowerShell against one another to see if there was any real difference when copying a single data set to a new location.


A took a sample of1824 files in 17 folders which came to 7.13 GB, and ran commands to transfer them from an external solid state drive to the C drive of a Dell XPS laptop with 16 GB of RAM. There was little difference in the elapsed time for each method, so the thing to do is to keep it simple and use Explorer. I guess the jury is out as to whether robocopy would work faster on a network. I will try to test this soon . . .


Windows Explorer 1 minute 9 seconds


robocopy 1 minute 14 seconds


PowerShell 1 minute 14 seconds

xcopy 1 minute 14 seconds



Note that when using robocopy it's necessary to share the source folder, and run the command in the admin mode of command prompt.



When setting the location for an external hard drive used as a source, copy the network path located under Properties. The device itself should have an alphanumeric code something like H4L34V8 - the code for my drive has been redacted in these screen grabs.










Sean O'Shea has more than 20 years of experience in the litigation support field with major law firms in New York and San Francisco.   He is an ACEDS Certified eDiscovery Specialist and a Relativity Certified Administrator.

​

The views expressed in this blog are those of the owner and do not reflect the views or opinions of the owner’s employer.

​

If you have a question or comment about this blog, please make a submission using the form to the right. 

Your details were sent successfully!

© 2015 by Sean O'Shea . Proudly created with Wix.com

bottom of page