Skip to content

Instantly share code, notes, and snippets.

@SonOfDiablo
Last active January 31, 2023 02:37
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save SonOfDiablo/81f3d610295c69c777b512e4da90393d to your computer and use it in GitHub Desktop.
Save SonOfDiablo/81f3d610295c69c777b512e4da90393d to your computer and use it in GitHub Desktop.
A script to download and unzip geckodriver, and then add it to the user path.
$ProgressPreference='SilentlyContinue'
Write-Host "Finding OS Architecture: " -ForegroundColor DarkCyan -NoNewline
# Find Os Architecture
if((Get-ComputerInfo).OsArchitecture -eq "64-bit"){
$arch = "*win64*"
Write-Host "64-bit" -ForegroundColor Green
}else{
$arch = "*win32*"
Write-Host "32-bit" -ForegroundColor Green
}
Write-Host "Making Temp Directory: " -ForegroundColor DarkCyan -NoNewline
# Generate random 10 char string
$tmp = -join (((48..57) + (65..90) + (97..122)) * 120 | Get-Random -Count 10 | ForEach-Object {[char]$_})
# Make temp directory
$tmpDir = "$env:TEMP\tmp.$tmp"
mkdir $tmpDir *>$null
Write-Host "$tmpDir" -ForegroundColor Green
Write-Host "Downloading zip: " -ForegroundColor DarkCyan -NoNewline
# Download zip
$tmpLocation = "$tmpDir\geckodriver.zip"
$downloadUrl = (((Invoke-WebRequest -Uri "https://api.github.com/repos/mozilla/geckodriver/releases/latest" -UseBasicParsing).content | ConvertFrom-Json).assets | Where-Object name -Like $arch).browser_download_url
Invoke-WebRequest -Uri $downloadUrl -OutFile $tmpLocation -UseBasicParsing
Write-Host "Done!" -ForegroundColor Green
Write-Host "Extracting exe: " -ForegroundColor DarkCyan -NoNewline
# Unzip
Expand-Archive -LiteralPath $tmpLocation -DestinationPath $tmpDir
Write-Host "Done!" -ForegroundColor Green
Write-Host "Checking if gecodriver already exists: " -ForegroundColor DarkCyan -NoNewline
$installDir = "$env:APPDATA\geckodriver"
$shouldCopy = $true
if(Test-Path "$installDir\geckodriver.exe"){
Write-Host "gecodriver already exists" -ForegroundColor DarkYellow
Write-Host "Checking file hashes: " -ForegroundColor DarkCyan -NoNewline
if((Get-FileHash "$installDir\geckodriver.exe").hash -eq (Get-FileHash "$tmpDir\geckodriver.exe").hash){
Write-Host "Hashes match!" -ForegroundColor DarkYellow
$shouldCopy = $false
}else{
Write-Host "Hashes doesn't match" -ForegroundColor Green
Write-Host "Removing old exe: " -ForegroundColor DarkCyan -NoNewline
Remove-Item -LiteralPath "$installDir\geckodriver.exe" -Force
Write-Host "Done!" -ForegroundColor Green
}
}else{
Write-Host "gecodriver doesn't exist" -ForegroundColor Green
Write-Host "Creating install directory in AppData: " -ForegroundColor DarkCyan -NoNewline
if(!(Test-Path $installDir)){
mkdir $installDir *>$null
}
Write-Host "Done!" -ForegroundColor Green
}
if($shouldCopy){
# Copy exe from temp directory to install directory
Write-Host "Copying exe: " -ForegroundColor DarkCyan -NoNewline
Copy-Item -LiteralPath "$tmpDir\geckodriver.exe" -Destination "$installDir\geckodriver.exe"
Write-Host "Done!" -ForegroundColor Green
}
Write-Host "Cleaning Up: " -ForegroundColor DarkCyan -NoNewline
# Clean Up
Remove-Item -LiteralPath $tmpDir -Force -Recurse
Write-Host "Done!" -ForegroundColor Green
Write-Host "Adding gecodriver to Path: " -ForegroundColor DarkCyan -NoNewline
# Adding gecodriver to Path
$userPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
if($userPath -like "*;$installDir*"){
Write-Host "gecodriver already in Path" -ForegroundColor DarkYellow
}else{
[System.Environment]::SetEnvironmentVariable("Path", "$userPath;$installDir", "User")
Write-Host "Done!" -ForegroundColor Green
Write-Host "Updating session Path: " -ForegroundColor DarkCyan -NoNewline
# Update session Path
$env:Path = "$env:Path;$installDir"
Write-Host "Done!" -ForegroundColor Green
}
$ProgressPreference='SilentlyContinue'
if((Get-ComputerInfo).OsArchitecture -eq "64-bit"){
$arch = "*win64*"
}else{
$arch = "*win32*"
}
$tmp = -join (((48..57) + (65..90) + (97..122)) * 120 | Get-Random -Count 10 | ForEach-Object {[char]$_})
$tmpDir = "$env:TEMP\tmp.$tmp"
mkdir $tmpDir *>$null
$tmpLocation = "$tmpDir\geckodriver.zip"
$downloadUrl = (((Invoke-WebRequest -Uri "https://api.github.com/repos/mozilla/geckodriver/releases/latest" -UseBasicParsing).content | ConvertFrom-Json).assets | Where-Object name -Like $arch).browser_download_url
Invoke-WebRequest -Uri $downloadUrl -OutFile $tmpLocation -UseBasicParsing
Expand-Archive -LiteralPath $tmpLocation -DestinationPath $tmpDir
$installDir = "$env:APPDATA\geckodriver"
$shouldCopy = $true
if(Test-Path "$installDir\geckodriver.exe"){
if((Get-FileHash "$installDir\geckodriver.exe").hash -eq (Get-FileHash "$tmpDir\geckodriver.exe").hash){
$shouldCopy = $false
}else{
Remove-Item -LiteralPath "$installDir\geckodriver.exe" -Force
}
}else{
if(!(Test-Path $installDir)){
mkdir $installDir *>$null
}
}
if($shouldCopy){
Copy-Item -LiteralPath "$tmpDir\geckodriver.exe" -Destination "$installDir\geckodriver.exe"
}
Remove-Item -LiteralPath $tmpDir -Force -Recurse
$userPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
if($userPath -like "*;$installDir*"){
}else{
[System.Environment]::SetEnvironmentVariable("Path", "$userPath;$installDir", "User")
$env:Path = "$env:Path;$installDir"
}
@TheMaddMaverick
Copy link

TheMaddMaverick commented Apr 15, 2020

I have two comments

  1. Which script do I need?
  2. I keep getting back a syntax error on both.

@SonOfDiablo
Copy link
Author

  1. Both do the same, the first one just has comments and writes out it's steps to the console.

  2. Can you share the error you get? and what OS/version and Powershell version do you use?

@not-tivia
Copy link

https://i.imgur.com/yXI6G2z.png
I have no idea what im doing, I assumed all I had to do was download it and right click Run with powershell and ended up with this

@SonOfDiablo
Copy link
Author

i.imgur.com/yXI6G2z.png
I have no idea what im doing, I assumed all I had to do was download it and right click Run with powershell and ended up with this

I'm not sure what the issue is.
Can you try open the script in a text editor and remove the very first line $ProgressPreference=’SilentlyContinue’

@not-tivia
Copy link

Seems it worked after removing the first line, thanks!

@SonOfDiablo
Copy link
Author

Seems it worked after removing the first line, thanks!

Happy I could help :)
Sorry I couldn't tell you what the underlying issue was though :/

@NoozAbooz
Copy link

How do i run this?

@SonOfDiablo
Copy link
Author

SonOfDiablo commented Sep 27, 2020

How do i run this?

Start PowerShell CLI:
Windows Key + R
Type "powershell.exe"

Then navigate to where you have downloaded the script to:
Type "cd Path/To/Script" (example C:\Users\$env:UserName\Downloads)

Then run the script:
Type the name of the script (example download_geckodriver.ps1)

and that should do it :)

@SonOfDiablo
Copy link
Author

Just updated the gist,

Turns out there was an issue with the single quotes used in the first line (’ instead of '), which caused the issue @bolsilent was having.

Thanks to @Spifffff who saw it and forked this gist with the fix :)

@NoozAbooz
Copy link

NoozAbooz commented Sep 27, 2020

Are these errors important?

PS C:\Users\Michael\Downloads\Free-Games> .\download_geckodriver.ps1 Finding OS Architecture: 64-bit Making Temp Directory: C:\Users\Michael\AppData\Local\Temp\tmp.F0ypRGJPit Downloading zip: Invoke-WebRequest : The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again. At C:\Users\Michael\Downloads\Free-Games\download_geckodriver.ps1:24 char:19

  • ... oadUrl = (((Invoke-WebRequest -Uri "https://api.github.com/repos/mozi ...
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotImplemented: (:) [Invoke-WebRequest], NotSupportedException
    • FullyQualifiedErrorId : WebCmdletIEDomNotSupportedException,Microsoft.PowerShell.Commands.InvokeWebRequestComman
      d

Invoke-WebRequest : Cannot validate argument on parameter 'Uri'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At C:\Users\Michael\Downloads\Free-Games\download_geckodriver.ps1:25 char:24

  • Invoke-WebRequest -Uri $downloadUrl -OutFile $tmpLocation -UseBasicPa ...
  •                    ~~~~~~~~~~~~
    
    • CategoryInfo : InvalidData: (:) [Invoke-WebRequest], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Done!
Extracting exe: Expand-Archive : The path 'C:\Users\Michael\AppData\Local\Temp\tmp.F0ypRGJPit\geckodriver.zip' either does not exist
or is not a valid file system path.
At C:\Users\Michael\Downloads\Free-Games\download_geckodriver.ps1:30 char:1

  • Expand-Archive -LiteralPath $tmpLocation -DestinationPath $tmpDir
  •   + CategoryInfo          : InvalidArgument: (C:\Users\Michae...geckodriver.zip:String) [Expand-Archive], InvalidOpe
     rationException
      + FullyQualifiedErrorId : ArchiveCmdletPathNotFound,Expand-Archive
    
    

Done!
Checking if gecodriver already exists: gecodriver doesn't exist
Creating install directory in AppData: Done!
Copying exe: Copy-Item : Cannot find path 'C:\Users\Michael\AppData\Local\Temp\tmp.F0ypRGJPit\geckodriver.exe' because it does not
exist.
At C:\Users\Michael\Downloads\Free-Games\download_geckodriver.ps1:60 char:5

  • Copy-Item -LiteralPath "$tmpDir\geckodriver.exe" -Destination "$i ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ObjectNotFound: (C:\Users\Michae...geckodriver.exe:String) [Copy-Item], ItemNotFoundExce
      ption
    • FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

Done!
Cleaning Up: Done!
Adding gecodriver to Path: gecodriver already in Path

@SonOfDiablo
Copy link
Author

SonOfDiablo commented Sep 28, 2020

@mobilegmyt the error indicates that Internet Explorer isn't installed (or at least not configured on your PC).
If you for some reason don't have IE on your PC try changing line 24 from
$downloadUrl = (((Invoke-WebRequest -Uri "https://api.github.com/repos/mozilla/geckodriver/releases/latest").content | ConvertFrom-Json).assets | Where-Object name -Like $arch).browser_download_url
to
$downloadUrl = (((Invoke-WebRequest -Uri "https://api.github.com/repos/mozilla/geckodriver/releases/latest" -UseBasicParsing).content | ConvertFrom-Json).assets | Where-Object name -Like $arch).browser_download_url

If you do have IE installed you might just not have ever ran it, in that case you should be good with just running IE once without changing the script.

Please let me know which of the two options you used and if it worked for you :)

@NoozAbooz
Copy link

This should mean it worked right? Gonna try testing the epic games redeemer again.

Finding OS Architecture: 64-bit
Making Temp Directory: C:\Users\Michael\AppData\Local\Temp\tmp.H5nbgXnNfb
Downloading zip: Done!
Extracting exe: Done!
Checking if gecodriver already exists: gecodriver doesn't exist
Creating install directory in AppData: Done!
Copying exe: Done!
Cleaning Up: Done!
Adding gecodriver to Path: gecodriver already in Path
PS C:\Users\Michael\Downloads\Free-Games>

@SonOfDiablo
Copy link
Author

@mobilegmyt

Yup that looks like it works fine :)

@NoozAbooz
Copy link

Maybe should change the script as described in https://gist.github.com/SonOfDiablo/81f3d610295c69c777b512e4da90393d#gistcomment-3469500 cuz no one uses IE lol.

@SonOfDiablo
Copy link
Author

@mobilegmyt Just did that.
I didn't know this was an issue until now, I usually use IE at least once to download another browser.

@Ohihahaha
Copy link

PS D:\Download\Edge> .\download_geckodriver_clean.ps1
PS D:\Download\Edge> .\download_geckodriver.ps1
Finding OS Architecture: 32-bit
Making Temp Directory: C:\Users\86765\AppData\Local\Temp\tmp.RdllkE4nO5
Downloading zip: Done!
Extracting exe: Done!
Checking if gecodriver already exists: gecodriver already exists
Checking file hashes: Hashes match!
Cleaning Up: Done!
Adding gecodriver to Path: gecodriver already in Path
PS D:\Download\Edge>

is this right?

@Ohihahaha
Copy link

PS D:+下载+边缘>.\download_geckodriver_clean.ps1 PS D:+下载+边缘>[download_geckodriver.ps1查找操作系统架构:32 位制作温度目录:C:=用户]86765]应用数据本地\tmp。Rdllke4no5下载拉链: 完成!提取 exe: 完成!检查格子驱动器是否已经存在: 格子司机已经存在检查文件哈希: 哈希匹配!清理:完成!将格子驱动器添加到路径:已经在路径PS D 中的格子驱动者:+下载+边缘>

这是对的吗?

first download_geckodriver_clean.ps1
then download_geckodriver.ps1

@SonOfDiablo
Copy link
Author

@Ohihahaha the two scripts does the same thing, Clean just doesn't have comments and output to console (It's less verbose).

It does look like the scripts ran correctly however :)

@i486
Copy link

i486 commented Jan 5, 2022

Windows 7 SP1 x64
PS 5.1

PS C:\Users\PC> Set-ExecutionPolicy RemoteSigned

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): y
PS C:\Users\PC> C:\81f3d610295c69c777b512e4da90393d-3bf37d72a678c5d3934355937e84e9928de4ad09\81f3d610295c69c777b512e4da9
0393d-3bf37d72a678c5d3934355937e84e9928de4ad09\download_geckodriver.ps1
Finding OS Architecture: Get-ComputerInfo : Unable to find an entry point named 'GetFirmwareType' in DLL 'kernel32.dll'.

At C:\81f3d610295c69c777b512e4da90393d-3bf37d72a678c5d3934355937e84e9928de4ad09\81f3d610295c69c777b512e4da90393d-3bf37d
72a678c5d3934355937e84e9928de4ad09\download_geckodriver.ps1:5 char:5
+ if((Get-ComputerInfo).OsArchitecture -eq "64-bit"){
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ComputerInfo], EntryPointNotFoundException
    + FullyQualifiedErrorId : System.EntryPointNotFoundException,Microsoft.PowerShell.Commands.GetComputerInfoCommand

Making Temp Directory: C:\Users\PC\AppData\Local\Temp\tmp.QZUXAK4dHR
Downloading zip: Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel.
At C:\81f3d610295c69c777b512e4da90393d-3bf37d72a678c5d3934355937e84e9928de4ad09\81f3d610295c69c777b512e4da90393d-3bf37d
72a678c5d3934355937e84e9928de4ad09\download_geckodriver.ps1:24 char:19
+ ... oadUrl = (((Invoke-WebRequest -Uri "https://api.github.com/repos/mozi ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Invoke-WebRequest : Cannot validate argument on parameter 'Uri'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At C:\81f3d610295c69c777b512e4da90393d-3bf37d72a678c5d3934355937e84e9928de4ad09\81f3d610295c69c777b512e4da90393d-3bf37d
72a678c5d3934355937e84e9928de4ad09\download_geckodriver.ps1:25 char:24
+ Invoke-WebRequest -Uri $downloadUrl -OutFile $tmpLocation -UseBasicPa ...
+                        ~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-WebRequest], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Done!
Extracting exe: Expand-Archive : The path 'C:\Users\PC\AppData\Local\Temp\tmp.QZUXAK4dHR\geckodriver.zip' either does no
t exist or is
not a valid file system path.
At C:\81f3d610295c69c777b512e4da90393d-3bf37d72a678c5d3934355937e84e9928de4ad09\81f3d610295c69c777b512e4da90393d-3bf37d
72a678c5d3934355937e84e9928de4ad09\download_geckodriver.ps1:30 char:1
+ Expand-Archive -LiteralPath $tmpLocation -DestinationPath $tmpDir
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (C:\Users\PC\App...geckodriver.zip:String) [Expand-Archive], InvalidOpe
   rationException
    + FullyQualifiedErrorId : ArchiveCmdletPathNotFound,Expand-Archive

Done!
Checking if gecodriver already exists: gecodriver doesn't exist
Creating install directory in AppData: Done!
Copying exe: Copy-Item : Cannot find path 'C:\Users\PC\AppData\Local\Temp\tmp.QZUXAK4dHR\geckodriver.exe' because it doe
s not exist.
At C:\81f3d610295c69c777b512e4da90393d-3bf37d72a678c5d3934355937e84e9928de4ad09\81f3d610295c69c777b512e4da90393d-3bf37d
72a678c5d3934355937e84e9928de4ad09\download_geckodriver.ps1:60 char:5
+     Copy-Item -LiteralPath "$tmpDir\geckodriver.exe" -Destination "$i ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\PC\App...geckodriver.exe:String) [Copy-Item], ItemNotFoundExce
   ption
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

Done!
Cleaning Up: Done!
Adding gecodriver to Path: gecodriver already in Path

@SonOfDiablo
Copy link
Author

@i486 I'm sorry this is script was written and tested on Windows 10.
I'm not sure if it works on Windows 7 and I don't have what it takes to test it.

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