This method uses the PowerShell version 2 onward compatible and uses System.Net.WebClient class.
Creates an array then adds required URL's to the array. Specify your download location, then for each item in your array, generate a filename which the System.Net.WebClient file parameter requires by using the PowerShell -Split Operator for the string from the last occurrence of the forward slash.
$Urls = @()
$Urls += "https://your/first/download/path/.ext"
$Urls += "https://your/next/download/path/.ext"
$OutPath = "C:\Path\to\download\"
ForEach ( $item in $Urls) {
$file = $OutPath + ($item).split('/')[-1]
(New-Object System.Net.WebClient).DownloadFile($item, $file)
}
Alternatively use the Invoke-WebRequest as per @McDonald's comment or the Invoke-WebRequest alias wget which are both available from PowerShell version 3 onwards but may be slower
$Urls = @()
$Urls += "https://your/first/download/path/.ext"
$Urls += "https://your/next/download/path/.ext"
$OutPath = "C:\Path\to\download\"
ForEach ( $item in $Urls) {
$file = $OutPath + ($item).split('/')[-1]
Invoke-WebRequest -Uri $item -Outfile $file
}
Save either script as "C:\Path\to\download_script.ps1"
create and run .bat file with the content
PowerShell -File "C:\Path\to\download_script.ps1"
or run the .ps1 file from PowerShell
EDIT
To accommodate your comment into my answer you can change the array into a multi column array and call the item heading as a . attribute
$Urls = @()
$item = "" | Select path,outpath,name
$item.path = "https://your/first/download/path/.ext"
$item.outpath = "C:\Path\to\download\"
$item.name = "Name1.ext"
$Urls = $Urls + $item
$item = "" | Select path,outpath,name
$item.path = "https://your/next/download/path/.ext"
$item.outpath = "C:\Path\to\download\"
$item.name = "Name2.ext"
$Urls = $Urls + $item
ForEach ( $item in $Urls) {
$file = ($item.outpath) + ($item.name)
(New-Object System.Net.WebClient).DownloadFile($item.path, $file)
}
Best Answer
In batch,
%~dpn0
returns the Drive, Path and Name of the currently executing script.To do the same in a PowerShell script you can use
$MyInvocation.MyCommand.Definition
.eg:
To get just the path to the script you could use:
(Note: in Powershell v3+ you can get the script's path (without the name) by referencing the predefined variable
$PSScriptRoot
)To get just the name of the script:
More info on Split-Path's options: https://technet.microsoft.com/en-us/library/hh849809.aspx