Download All PDF File From MS Site URL Using Powershell

Microsoft has recently released a FREE MICROSOFT EBOOK GIVEAWAY extravaganza! And this time MORE FREE EBOOKS. If you are like me you want to download all pdf documents in one go

Here you have PowerShell script which will download all the pdf files (246) in a desktop folder.



Here is the script,

  1. $url = "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/"  
  2. $r1 = Invoke - WebRequest - Uri $url  
  3. $DesktopPath = [Environment]::GetFolderPath("Desktop")  
  4. $path = $DesktopPath + "\PDFDownloads"  
  5. If(!(test - path $path)) {  
  6.     New - Item - ItemType Directory - Force - Path $path  
  7. }  
  8. $regex = '([a-zA-Z]{3,})://([\w-]+\.)+([\w-]+(/[\w- ./?%&=]*)*?)+[a-z0-9.\-]'  
  9. $r1.ParsedHtml.getElementsByTagName('td') | Where - Object - Property innerText - Match 'PDF' | select - object - ExpandProperty innerHTML | Out - File $path\ innerhtml.txt  
  10. $files = select - string - path $path\ innerhtml.txt - Pattern $regex - AllMatches | % {  
  11.     $_.Matches  
  12. } | % {  
  13.     $_.Value  
  14. }  
  15. ForEach($file in $files) {  
  16.     $request = Invoke - WebRequest - Uri $file - MaximumRedirection 0 - ErrorAction Ignore  
  17.     $FileName = $request.Headers.Location  
  18.     if ($FileName - match '.pdf') {  
  19.         $output = $FileName.SubString($FileName.LastIndexOf('/') + 1)  
  20.         Invoke - WebRequest - Uri $FileName - OutFile $path\ $output  
  21.     }  
  22. }  
  23. If(Test - Path $path\ innerhtml.txt) {  
  24.     Remove - Item $path\ innerhtml.txt  
  25. }