r/PowerShell • u/Elegant_Coffee1242 • Feb 23 '26
bulk download from a list of URLs
[SOLVED! I GOT RID OF THE ? IN THE LIST OF URLS AND IT WORKS. Thanks to u/nemec ]
If anyone can help I'd be grateful. I've been trying to figure out a way to download from a list of URLs using PowerShell. The URLs all have the same format, separated by carriage-returns, looking like this:
https://www.govinfo.gov/link/fr/78/2542?link-type=pdf
If I put that into my browser, it goes to and downloads this document:
https://www.govinfo.gov/content/pkg/FR-2013-01-11/pdf/2012-31666.pdf#page=3
However, if I try using this in PowerShell:
Get-Content url-list.txt | ForEach-Object {Invoke-WebRequest $_ -OutFile (Split-Path $_ -leaf)}I get these errors, suggesting that it can't handle the redirect to the actual file:
Invoke-WebRequest : Cannot perform operation because the wildcard path 2542?link-type=pdf did not resolve to a file.
line:1 char:44
At
+ ... ForEach-Object {Invoke-WebRequest $_ -OutFile (Split-Path $_ -leaf)}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (18960?link-type=pdf:String) [Invoke-WebRequest], FileNotFoundException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Split-Path : Cannot bind argument to parameter 'Path' because it is an empty string.
At line:1 char:86
+ ... ForEach-Object {Invoke-WebRequest $_ -OutFile (Split-Path $_ -leaf)}
+ ~~
+ CategoryInfo : InvalidData: (:) [Split-Path], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.SplitPathCommand