Knowing PowerShell can come in handy when you need to download files. Invoke-WebRequest is the command to get to know when working with web parsing, and obtaining downloads.
I've noticed, however, that different files show up as different content types, and parsing out the file name requires all sorts of voodoo. What if there was a way to use one tool that could utilize the power of PowerShell, and make downloading files a modular experience?
This tool, and blog post, are is inspired by folks asking me for help downloading files via PowerShell. I always appreciate feedback and questions, and this is exactly why!
In this post
Ninja Downloader works by executing the main script (download.ps1), which takes the following parameters:
There are several scripts included by default with tool.
So how do we use them, then?
This will give us access to the results in $downloadResults.
You can see that the results we get include the name of the script executed, if it was executed successfully, any errors, and another object inside of this object named FileInfo.
FileInfo contains the file name, path to the file (full), any errors, and if we could verify it exists.
This attempt was successful, and our results echoed that! Let's take a look in the downloads folder just to be sure...
Awesome!
What if we wanted to download all the files via every script in the .\scripts folder?
This time we can see some of the output as it happens via the -Verbose switch.
Now let's take a look at $downloadResults:
For good measure, we'll also look at the downloads folder:
Alright! It worked.
This script allows you output the results in various ways. All of the results will be time-stamped with the date and time.
To output results as a CSV, run:
After it runs, the results will be in the .\output folder.
To output results as HTML, run:
After it runs, the results will be in the .\output folder.
To output results as XML, run:
After it runs, the results will be in the .\output folder.
To output results in all three formats, run:
After it runs, the results will be in the .\output folder.
You can create your own script to use with the Ninja Downloader tool. The template provided is a working example of how Firefox is downloaded. The template is located in the .\scripts folder.
Template code:
What matters the most is that you return an object that has the following properties:
Example script creation:
For this example, let's download ElvUI (as we learned how to in detail, here).
First, I'll save the template as elvui.ps1
Then, based on our knowledge of web parsing (more here if you would like to learn more about Invoke-WebRequest), we can edit the template to reflect the correct web parsing needs and content type.
Full code for elvui.ps1
Let's test it out!
And there you have it, modular file downloading via PowerShell.
This project is available on Github!
Click here to go to the psNinjaDownloader repository.
You can download the contents as a ZIP file by going to 'Clone or download', and then selecting 'Download ZIP'.
If you download the code, and would like to run it, you'll need to unblock download.ps1 first.
To do this, right click download.ps1, and go to Properties, and check 'unblock', then click Apply/OK.
You'll also need to do this for all the scripts in the .\scripts folder
You can repeat the above step for every script in the .\scripts folder, or we can use PowerShell to do it!
Voila, you now have a working copy of the script!
Get-Help .\download.ps1 -Full
If you have any feedback, questions, or issues, leave a comment here or contact me!
No comments yet. Be the first!