Auto-Login to Multiple KeePass Databases

Depending on the need, I use a couple of different password managers. For many things I use 1Password, primarily for it's multi-user support. Some a few things I use a couple of KeePass databases. I log into the two KeePass databases everyday, and the manual work of getting all of it open became tiresome.

The process I followed everyday:

  1. Open KeePass
  2. Login to which ever account I last logged into1
  3. Then File > Open, navigate to and select my second database2
  4. Login to the second database

While doing this once a day wasn't the end of the world, it was time consuming. I decided to automate the launch and login of both KeePass files.

First, a couple of caveats.

  1. This tutorial assumes you use two or more KeePass databases
  2. My scripts contain passwords in plain text3
  3. Depending on your use case you should explore encryption4 or at least some form of obfuscation5

Overview of Solution

  • A batch file that launches KeePass and a VBScript file
  • A VBScript file that automates the selection and login of two databases

Step 0

Take inventory of the following:

  • Name and Directory of KeePass database 1
  • Name and Directory of KeePass database 2
  • Directory of KeePass installation (where KeePass.exe is located)

Step 1

Disable the setting that causes KeePass to remember the last database.

The setting is found in the Advanced settings.

Tools | Options | Advanced
  1. Locate a checkbox labelled: "Remember and automatically open last used database on startup".
  2. Uncheck this setting.

Step 2

Create a batch file to open KeePass and launch the vbscript that will actually do the work.

This is an example of the batch file that includes comments (lines with REM) that explain what each part does. See further below for the script without comments.

@echo off
REM COMMENT 
REM Change Directory to KeePass installation location
REM run KeePass executable

cd "F:\z-Apps\KeePass"
start KeePass.exe

REM COMMENT 
REM Allow 2 seconds for program to launch

timeout /t 2

REM COMMENT
REM Change Directory to vbs file location
REM run ("CALL") vbs script

cd "F:\z-Scripts"
CALL Multi-KeePass.vbs

exit

Here is what your batch file will actually look like:

@echo off
cd "C:\Folder\KeePassFolder"
start KeePass.exe

timeout /t 2

cd "C:\Folder\ScriptsFolder"
CALL Multi-KeePass.vbs

exit

To create your own version:

  1. Create a text file in Notepad or Notepad++ and copy and paste the script without comments above into it.
  2. Save the file as a name of your choosing (I used "Multi-KeePass")
  3. Change the file extension from .txt to .bat
  4. Open the file and Change the C:\Folder\KeePassFolder to the KeePass directory on your own computer (i.e., folder that contains the KeePass.exe file)
  5. The C:\Folder\ScriptsFolder and Multi-KeePass.vbs will changed later after the VBScript file has been created.

Step 3

Create a VBScript file that will do the work of logging into both KeePass databases automatically when the related batch file is launched.

This is an example of the VBS file that includes comments (lines with ' ) that explain what each part does. See further below for the script without comments.

set wshshell = wscript.CreateObject("wscript.shell")

' COMMENT
' Target the open KeePass application
' Open new file window
' Input path into name field
' Input file name into name field to select file

wshshell.AppActivate "KeePass"
wshShell.SendKeys ("^o")
wshShell.SendKeys "C:\Folder\SubFolder1{ENTER}"
wshShell.SendKeys "KeePass_1.kdbx{ENTER}"

' COMMENT
' Target window requesting password
' Input password (Replace PASSWORD with your password)

wshshell.AppActivate "Open Database - KeePass_1.kdbx"
wshShell.SendKeys "PASSWORD{ENTER}"

' COMMENT
' Target KeePass application window with new name
' Open new file window
' Input path into name field
' Wait/sleep for a few moments
' Input file name into name field to select file

wshshell.AppActivate "KeePass_1.kdbx - KeePass"
wshShell.SendKeys ("^o")
wshShell.SendKeys "C:\Folder\SubFolder2{ENTER}"
wscript.sleep 3000
wshShell.SendKeys "KeePass_2.kdbx{ENTER}"

' COMMENT
' Target window requesting password
' Input password (Replace PASSWORD with your password)

wshshell.AppActivate "Open Database - KeePass_2.kdbx"
wshShell.SendKeys "PASSWORD{ENTER}"

Here is what your VBS file will actually look like:

set wshshell = wscript.CreateObject("wscript.shell")
wshshell.AppActivate "KeePass"
wshShell.SendKeys ("^o")
wshShell.SendKeys "C:\Folder\SubFolder1{ENTER}"
wshShell.SendKeys "KeePass_1.kdbx{ENTER}"
wshshell.AppActivate "Open Database - KeePass_1.kdbx"
wshShell.SendKeys "PASSWORD{ENTER}"
wshshell.AppActivate "KeePass_1.kdbx - KeePass"
wshShell.SendKeys ("^o")
wshShell.SendKeys "C:\Folder\SubFolder2{ENTER}"
wscript.sleep 3000
wshShell.SendKeys "KeePass_2.kdbx{ENTER}"
wshshell.AppActivate "Open Database - KeePass_2.kdbx"
wshShell.SendKeys "PASSWORD{ENTER}"

To create your own version:

  1. Create a text file in Notepad or Notepad++ and copy and paste the script without comments above into it.
  2. Save the file as a name of your choosing (I used "Multi-KeePass")
  3. Change the file extension from .txt to .vbs
  4. Open the newly created file and reference the text below to replace the bold sections with your own information:

set wshshell = wscript.CreateObject("wscript.shell")
wshshell.AppActivate "KeePass"
wshShell.SendKeys ("o")
wshShell.SendKeys "C:\Folder\SubFolder1{ENTER}"
wshShell.SendKeys "KeePass_1.kdbx{ENTER}"
wshshell.AppActivate "Open Database - KeePass_1.kdbx"
wshShell.SendKeys "PASSWORD{ENTER}"
wshshell.AppActivate "KeePass_1.kdbx - KeePass"
wshShell.SendKeys ("o")
wshShell.SendKeys "C:\Folder\SubFolder2{ENTER}"
wscript.sleep 3000
wshShell.SendKeys "KeePass_2.kdbx{ENTER}"
wshshell.AppActivate "Open Database - KeePass_2.kdbx"
wshShell.SendKeys "PASSWORD{ENTER}"

Breakdown:

  • C:\Folder\SubFolder1: Replace the single instance with the directory of your first KeePass database
  • KeePass_1: Replace all three instances with the name of your first KeePass database
  • PASSWORD: (first instance) with the password of your first KeePass database
  • C:\Folder\SubFolder2: Replace the single instance with the directory of your second KeePass database
  • KeePass_2: Replace both instances with the name of your second KeePass database
  • PASSWORD: (second instance) with the password of your second KeePass database

Step 4

Run two instances of KeePass -- which means different bat and vbs files. This is in Sourceforge

--edit to create different shortcut keys for each one.


  1. By default KeePass remembers the location of the last database you navigated to. If you regularly login to multiple databases the 'last' database you logged into will often be different. This means that  

  2. While the first database will open and prompt for login upon launch, any additional databases have to be specifically selected and launched. 

  3. These passwords are not super important, and they are on my own hardware that's not connected to the internet. In addition, the scripts that contain the actual passwords are on an encrypted flash drive, which is unplugged after launching KeePass. This is just a personal cost/benefit decision that I made. 

  4. How to safely store a password 

  5. vbs script obfuscate discussion 


You'll only receive email when theogeek publishes a new post

More from theogeek