Download OReilly free books with PowerShell through PasteBin

    [Parameter(Mandatory=$true)] [string]$DestinationFolder,
    [Parameter(Mandatory=$false)] [string]$OReillyPasteBinRawUrl = ‘‘,
    [Parameter(Mandatory=$false)] [bool]$SimulateSleepBetweenDownloads = $true

Import-Module BitsTransfer

function Download-File
    param([string] $UrlSource, [string] $Destination)
    $random = Get-Random -Minimum 1 -Maximum 5
    Write-Output “Downloading $filename”
    Start-BitsTransfer -Source $UrlSource -Destination $Destination
    if ($SimulateSleepBetweenDownloads) {
        Write-Output ”    Sleeping $(4 + $random) seconds”
        Start-Sleep -s (4 + $random)

$tmpFile = New-TemporaryFile
Set-Content -Path ($tmpFile.FullName) -Value ((Invoke-WebRequest -Uri $OReillyPasteBinRawUrl).Content)

$reader = [System.IO.File]::OpenText($tmpFile.FullName)
while($null -ne ($url = $reader.ReadLine())) {
    $filename = $URL.Substring($URL.LastIndexOf(“/”) + 1)
    $destPath = (Join-Path $DestinationFolder $filename)
    if (!(Test-Path $destPath)) {
        Download-File -UrlSource $url -Destination $destPath
    else {
        if ((Get-Item $destPath).Length -le 0) {
            Download-File -UrlSource $url -Destination $destPath
        else {
            Write-Output “Skipping $filename ; already downloaded”

After that, use the script like that:

.\downloadOreallyBooks.ps1 -DestinationFolder E:\temp\ -SimulateSleepBetweenDownloads $false

Note 1: Credits go to StackOverflow, IT blogs and alike, sites that helped me with the code when searching for solutions.
Note 2: Be aware that when copy-pasting, the quotes and possibly other characters get messed up by WordPress, you’ll have to replace them.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: