Skip to content

Instantly share code, notes, and snippets.

@guihkx
Created November 14, 2017 05:53
Show Gist options
  • Save guihkx/cc21fd33a43df82212f6f2b7cc5cf46d to your computer and use it in GitHub Desktop.
Save guihkx/cc21fd33a43df82212f6f2b7cc5cf46d to your computer and use it in GitHub Desktop.
#Requires -Version 5.0
# If you, for some odd reason, have a lot of different Firefox profiles (like I do),
# this script will create a shortcut for each one of them so you can use them easily
# Keep in mind that this script was made by a guy who didn't knew absolutely nothing about PowerShell
#----------------------------------------------------------------#
# This script will only work if you have Windows 7 SP1 or newer. #
# #
# If you have W7 SP1 but your PowerShell version is older than #
# 5.0, you need to update it: #
# #
# https://www.microsoft.com/en-us/download/details.aspx?id=50395 #
#----------------------------------------------------------------#
# Where do you want your shortcuts to be saved?
# $OUTPUT_DIR = "$HOME\Documents\Firefox"
function Get-Firefox-Exe {
# Possible registry keys to find the Firefox executable.
$fx_paths = @("HKLM:\SOFTWARE\Mozilla\Mozilla Firefox","HKLM:\SOFTWARE\Wow6432Node\Mozilla\Mozilla Firefox")
ForEach($fx_path in $fx_paths) {
$fx_key = Get-ChildItem -Path $fx_path -ErrorAction SilentlyContinue
if(!$fx_key) {
Continue
}
$fx_count = (Get-Item -Path $fx_path).SubKeyCount
$fx_exe = $Null
$ret = $Null
ForEach($key in $fx_key) {
$fx_exe = Get-ItemPropertyValue -Path "$($fx_path)\$($key.PSChildName)\Main" -Name PathToExe -ErrorAction SilentlyContinue
if($fx_exe) {
$ret = Test-Path $fx_exe
if($ret) {
Break
}
}
}
if($ret) {
Break
}
}
return $fx_exe
}
# Thanks to:
# https://stackoverflow.com/a/9701907
#
# $out_dir - Output directory
# $fx_exe - Firefox executable
# $profile - Profile name
function Create-Firefox-Shortcut($out_dir, $fx_exe, $profile) {
$wsh_shell = New-Object -comObject WScript.Shell
$shortcut = $wsh_shell.CreateShortcut("$($out_dir)\$($profile).lnk")
$shortcut.TargetPath = $fx_exe
$shortcut.Arguments = "-no-remote -P `"$($profile)`""
$shortcut.Save()
}
$fx_exe = Get-Firefox-Exe
if($fx_exe) {
$counter = 0
# Thanks to:
# http://www.powershellmagazine.com/2013/06/28/pstip-using-the-system-windows-forms-folderbrowserdialog-class/
Add-Type -AssemblyName System.Windows.Forms
$browser = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{
Description = "Where do you want to save your Firefox shortcuts?"
ShowNewFolderButton = $false
}
[void]$browser.ShowDialog()
if(!$browser.SelectedPath) {
Exit 1
}
# New-Item $OUTPUT_DIR -Type Directory -ErrorAction SilentlyContinue | Out-Null
Get-ChildItem -Path "$Env:APPDATA\Mozilla\Firefox\Profiles" -Directory |
ForEach-Object {
# https://support.mozilla.org/t5/tkb/articleprintpage/tkb-id/install-and-update-kb/article-id/132#w_restoring-a-profile-backup
# We have to skip the first 8 random characters, plus a dot
$profile_name = $_.Name.Substring(9)
Create-Firefox-Shortcut $browser.SelectedPath $fx_exe $profile_name
$counter++
}
if($counter -eq 0) {
Write-Error "No Firefox profiles were found."
Exit 1
}
else {
Write-Output "$counter shortcuts were created."
}
}
else {
Write-Error "Unable to find Firefox's executable."
Exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment