Last active
September 12, 2026 11:44
-
-
Save mesheets/c423e1c51427381b9db27efd6f30d1a0 to your computer and use it in GitHub Desktop.
Open Brave in Private Mode by Default
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Set shortcuts and registry entries to launch Brave in private mode | |
| # NOTE: This script will likely need to be executed as an administrator | |
| # in order to complete some of the updates. | |
| using namespace System.IO | |
| Set-StrictMode -Version Latest | |
| # Reference | |
| # https://www.vbsedit.com/html/d91b9d23-a7e5-4ec2-8b55-ef6ffe9c777d.asp | |
| # https://learn.microsoft.com/en-us/troubleshoot/windows-client/admin-development/create-desktop-shortcut-with-wsh | |
| # https://stackoverflow.com/questions/484560/editing-shortcut-lnk-properties-with-powershell | |
| # Registry entry as would be found in a *.reg file | |
| # Windows Registry Editor Version 5.00 | |
| # [HKEY_CLASSES_ROOT\BraveHTML\shell\open\command] | |
| # @="\"C:\\Program Files\\BraveSoftware\\Brave-Browser\\Application\\brave.exe\" --incognito --single-argument %1" | |
| # From comment at https://superuser.com/a/1456571: Execute script from the web | |
| # From https://stackoverflow.com/q/46073096: Generically link to latest Gist version | |
| # NOTE: The "subshell" is used in case the *.ps1 PowerShell script exits when finished | |
| # powershell -Command { Invoke-RestMethod https://gist.githubusercontent.com/mesheets/c423e1c51427381b9db27efd6f30d1a0/raw/Set-BravePrivate.ps1 | Invoke-Expression } | |
| # Constants | |
| $subpathTaskBar = '\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar' | |
| $registryPath = 'Registry::HKEY_CLASSES_ROOT\BraveHTML\shell\open\command' | |
| $executableName = 'Brave.exe' | |
| $shortcutName = 'Brave.lnk' | |
| $flagForPrivate = '--incognito' | |
| function Get-ShortcutFolders { | |
| # Generate an array of common shortcut folder paths | |
| [DirectoryInfo[]]$shortcutFolders = @( | |
| [Environment]::GetFolderPath('StartMenu'), | |
| [Environment]::GetFolderPath('CommonStartMenu'), | |
| [Environment]::GetFolderPath('DesktopDirectory'), | |
| [Environment]::GetFolderPath('CommonDesktopDirectory'), | |
| (Join-Path -Path ([Environment]::GetFolderPath('ApplicationData')) -ChildPath $subpathTaskBar), | |
| (Join-Path -Path ([Environment]::GetFolderPath('CommonApplicationData')) -ChildPath $subpathTaskBar) | |
| ) | |
| # Return only those paths that actually exist on the system running this script | |
| return $shortcutFolders.Where({$_.Exists}) | |
| } | |
| function Update-ShortcutArguments { | |
| param( | |
| [string]$shortcutFileName = $null, | |
| [string]$argument = $null | |
| ) | |
| $shell = New-Object -ComObject WScript.Shell | |
| [DirectoryInfo[]]$directories = Get-ShortcutFolders | |
| $path = dir $directories -Filter $shortcutFileName -Recurse | |
| $path | ForEach-Object { | |
| if ($_) { | |
| $shortcut = $shell.CreateShortcut($_.FullName) | |
| $info = @{} | |
| $info.Hotkey = $shortcut.Hotkey | |
| $info.TargetPath = $shortcut.TargetPath | |
| $info.LinkPath = $shortcut.FullName | |
| $info.Target = try {Split-Path $info.TargetPath -Leaf } catch { 'n/a' } | |
| $info.Link = try { Split-Path $info.LinkPath -Leaf } catch { 'n/a' } | |
| $info.WindowStyle = $shortcut.WindowStyle | |
| $info.IconLocation = $shortcut.IconLocation | |
| $info.Arguments_Before = $shortcut.Arguments | |
| if (-not $shortcut.Arguments) { | |
| # There are no arguments, so just add it | |
| $shortcut.Arguments = $argument | |
| } | |
| elseif ($shortcut.Arguments -iNotMatch $argument) { | |
| # Other arguments exist; safest is to add as the first argument | |
| $shortcut.Arguments = $argument + ' ' + $shortcut.Arguments | |
| } | |
| $info.Arguments_After = $shortcut.Arguments | |
| try { | |
| $shortcut.Save() | |
| $targetPath = $shortcut.TargetPath | |
| } catch { | |
| Write-Error "$_" | |
| } | |
| New-Object PSObject -Property $info | |
| } | |
| } | |
| } | |
| function Update-RegistryArguments { | |
| param( | |
| [string]$regPath = $null, | |
| [string]$exeName = $nulll, | |
| [string]$argument = $null | |
| ) | |
| $newRegValue = $null | |
| $regValue = (Get-ItemProperty -Path $regPath)."(default)" | |
| Write-Output "Registry value: $regValue" | |
| if ($regValue -iNotMatch $argument) { | |
| if ($regValue -iMatch "$exeName""") { | |
| $newRegValue = $regValue -iReplace "$exeName""", "$exeName"" $argument" | |
| } | |
| elseif ($regValue -iMatch $exeName) { | |
| $newRegValue = $regValue -iReplace $exeName, "$exeName $argument" | |
| } | |
| else { | |
| Write-Error "Unable to match registry value for updating" | |
| } | |
| if ($newRegValue) { | |
| Write-Output "Updated value: $newRegValue" | |
| Set-Item -Path $regPath -Value $newRegValue | |
| } | |
| else { | |
| Write-Output "Reg value unmodified" | |
| } | |
| } | |
| } | |
| Update-ShortcutArguments $shortcutName $flagForPrivate | |
| Update-RegistryArguments $registryPath $executableName $flagForPrivate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment