Skip to content

Instantly share code, notes, and snippets.

@joegasper
Last active December 25, 2024 21:34
Show Gist options
  • Save joegasper/a98d3fd973d3760ca1ae890f26523045 to your computer and use it in GitHub Desktop.
Save joegasper/a98d3fd973d3760ca1ae890f26523045 to your computer and use it in GitHub Desktop.
Download free Microsoft eBooks in massive list provided by Microsoft
#[1] https://blogs.msdn.microsoft.com/mssmallbiz/2016/07/10/how-to-download-all-of-the-free-ebooks-and-resources-in-my-free-ebooks-giveaway/
#[2] https://blogs.msdn.microsoft.com/mssmallbiz/2017/07/11/largest-free-microsoft-ebook-giveaway-im-giving-away-millions-of-free-microsoft-ebooks-again-including-windows-10-office-365-office-2016-power-bi-azure-windows-8-1-office-2013-sharepo/
#Some links are to webpages or Sway, I'd like to save the link to these pages - still to do.
$saveTo = 'D:\MS_eBooks\' #where to place the eBooks
New-Item -Path $saveTo -ItemType Directory
$masterListLink = 'http://ligman.me/29zpthb' #[1] link to the text file of links to the eBooks.
#$masterListLink = 'http://ligman.me/2sZVmcG' #[2] link to the text file of links to the eBooks.
#Get an array of links, triming the header in the first line.
$eBookLinks = @((Invoke-WebRequest -Uri $masterListLink -MaximumRedirection 1).Content -split "`n" | % {$_.trim()} | select -Skip 1 )
foreach ($eBookLink in $eBookLinks) {
try {
$request = Invoke-WebRequest -Uri $eBookLink -Method Head -ErrorAction Ignore #retrieve details on the eBook short URL
if($request.StatusCode -like '200') {
$eBookURL = $request.BaseResponse.ResponseUri.AbsoluteUri #final URL to eBook
$eBookTitle = $saveTo + [uri]::UnescapeDataString($request.BaseResponse.ResponseUri.Segments[-1]) #full path of eBook on disk
Write-Output "Saving: $eBookTitle"
Invoke-WebRequest -Uri $eBookURL -OutFile $eBookTitle
Unblock-File $eBookTitle
} else {
Write-Warning "Failed: $eBookLink"
}
}
catch {
Write-Warning "Failed: $eBookLink"
}
}
@athinaok
Copy link

`# Define the save location
$saveTo = 'Y:'

Create the directory if it doesn't exist

if (-not (Test-Path -Path $saveTo)) {
New-Item -Path $saveTo -ItemType Directory | Out-Null
}

Define the master list link

$masterListLink = 'http://ligman.me/29zpthb' #[1] link to the text file of eBook links

$masterListLink = 'http://ligman.me/2sZVmcG' #[2] alternate link to the text file of eBook links

Get an array of eBook links, trimming the header in the first line

$eBookLinks = @((Invoke-WebRequest -Uri $masterListLink -MaximumRedirection 1).Content -split "`n" | ForEach-Object { $_.Trim() } | Select-Object -Skip 1)

Loop through each eBook link

foreach ($eBookLink in $eBookLinks) {
try {
# Get eBook URL details
$request = Invoke-WebRequest -Uri $eBookLink -Method Head -ErrorAction Ignore

    if ($request.StatusCode -eq 200) {
        $eBookURL = $request.BaseResponse.ResponseUri.AbsoluteUri
        $eBookTitle = Join-Path -Path $saveTo -ChildPath ([uri]::UnescapeDataString($request.BaseResponse.ResponseUri.Segments[-1]))

        # Check if file already exists
        if (Test-Path -Path $eBookTitle) {
            Write-Output "File already exists, skipping: $eBookTitle"
            continue
        }

        # Download the eBook
        Write-Output "Saving: $eBookTitle"
        Invoke-WebRequest -Uri $eBookURL -OutFile $eBookTitle
        Unblock-File -Path $eBookTitle
    } 
    else {
        Write-Warning "Failed (HTTP Status: $($request.StatusCode)): $eBookLink"
    }
}
catch {
    Write-Warning "Failed: $eBookLink | Error: $_"
}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment