Windows 上如何用 CODEX_HOME 在 PowerShell、cmd 和 Windows Terminal 管理兩個 Codex CLI 帳戶
Two Codex accounts on Windows: CODEX_HOME in PowerShell, cmd and Windows Terminal
文章説明如何在 Windows 透過 CODEX_HOME 為 Codex CLI 指定不同資料夾,讓不同帳戶各自保留登入狀態。文中分別示範 PowerShell、cmd 和 Windows Terminal 的設定方式,並提醒不要用 setx 設定永久環境變數。
Disclosure up front: I build Sessions Pro for Codex, a paid app that does this for the desktop app too. Everything up to the last section is free and needs nothing but the commands below.
Most guides for running two Codex accounts are written for macOS and Linux: export CODEX_HOME=..., an alias in ~/.zshrc, done. On Windows the idea is the same, but the syntax isn't, and there are a few Windows-only details worth knowing. All the commands here were run on Windows 11.
How Codex decides which account you're signed in to
The Codex CLI doesn't keep a login per terminal window. It keeps one login per folder: CODEX_HOME. On Windows that's %USERPROFILE%\.codex unless you set the variable. The folder holds:
-
auth.json: the login -
config.toml: your settings, including MCP servers - your session history and threads
One folder, one account. Point CODEX_HOME at a second folder, sign in there once, and that folder stays signed in to the second account.
The ChatGPT desktop app from the Microsoft Store (its package is called OpenAI.Codex) uses the same %USERPROFILE%\.codex for its Codex data. So by default, codex in your terminal and Codex in the app share one login. Keep that in mind for codex logout, below. ChatGPT's built-in account switcher doesn't change this either: it works on chatgpt.com and doesn't touch the CLI's login.
Which codex are you running?
If you installed the CLI yourself (for example with npm install -g @openai/codex), codex is on your PATH. The Store app also ships its own codex.exe inside its package folder, and you can find it from PowerShell:
Join-Path (Get-AppxPackage OpenAI.Codex).InstallLocation 'app\resources\codex.exe'
Either one reads CODEX_HOME the same way.
PowerShell
Create the folder first. Codex won't create it for you:
New-Item -ItemType Directory -Force "$HOME\.codex-work" | Out-Null
Then start Codex pointed at it:
$env:CODEX_HOME = "$HOME\.codex-work"
codex
Sign in with your second account. From now on, any codex started from this PowerShell window uses .codex-work. Open a new window and plain codex is back on your first account.
To check where you stand before doing anything:
$env:CODEX_HOME # empty means the default %USERPROFILE%\.codex
codex login status # "Logged in using ChatGPT" or "Not logged in"
If you forget to create the folder, Codex stops with:
Error loading configuration: CODEX_HOME points to "C:\\Users\\you\\.codex-work", but that path does not exist
Make it one word
Add a function to your PowerShell profile (notepad $PROFILE, create the file if it doesn't exist):
function codex-work {
$previous = $env:CODEX_HOME
$env:CODEX_HOME = "$HOME\.codex-work"
try { codex @args } finally { $env:CODEX_HOME = $previous }
}
codex-work now runs Codex in the work account and puts the window back the way it was afterwards, so a later plain codex in the same window still uses your first account. Add one function per account.
cmd
In Command Prompt, set the variable for the session, then run Codex:
mkdir "%USERPROFILE%\.codex-work"
set "CODEX_HOME=%USERPROFILE%\.codex-work"
codex
Use the quoted form of set. Without quotes, set CODEX_HOME=... && codex stores the space before && as part of the value. Codex happened to cope with that in my tests, but a variable with an invisible trailing space is the kind of thing that breaks something else later.
A Windows Terminal profile per account
Windows Terminal makes this tidy: one profile per account, each opening already pointed at the right folder. In Settings, add a new profile and set its Command line to:
powershell.exe -NoExit -Command "$env:CODEX_HOME = Join-Path $HOME '.codex-work'; codex"
Name it something like "Codex (work)" and give it its own tab color, so you can tell at a glance which account a tab is using.
Don't set it permanently
It's tempting to run setx CODEX_HOME ... and be done. Don't: a user-level variable is inherited by every program you start afterwards, so every codex would use that folder and you'd be back to one account, just a different one. Set CODEX_HOME per window, per function or per Terminal profile instead.
What about WSL?
Inside WSL, ~ is your Linux home, not your Windows one. So codex running in WSL uses ~/.codex on the Linux side, a different folder from the Windows %USERPROFILE%\.codex. That makes WSL a second, separate login by accident. It's fine if you want it, confusing if you don't. The same CODEX_HOME trick works there with the usual export syntax.
codex logout and the shared folder
codex logout removes the login from the current CODEX_HOME. If that's the default folder, every codex reading it is signed out at once, and by default that includes Codex in the desktop app. With one folder per account, logging out of one leaves the others alone.
What to copy between folders
-
Copy
config.tomlif you want the same settings and MCP servers in both accounts:Copy-Item "$HOME\.codex\config.toml" "$HOME\.codex-work\" -
Don't copy
auth.json. That's the login. Copying it puts your first account into the second folder. - Leave history alone. Each folder keeps its own sessions, which is the point.
The desktop app is the hard part
CODEX_HOME only separates the command line. The ChatGPT desktop app keeps using its own data and %USERPROFILE%\.codex, so it still holds one account at a time.
On a Mac you can make a second copy of the app with its own data folder. On Windows the app comes from the Microsoft Store and lives in a protected package folder, so that route isn't really open. The free option on Windows is a second Windows user account: each user gets their own app data and their own .codex, and both can stay signed in if you switch users instead of signing out. It works well when two people share a PC, and it's clumsy for one person flipping between work and personal all day.
That gap is what Sessions Pro for Codex fills. Each account gets its own profile that runs the ChatGPT app you already have in its own window, with its own login, chats, Codex threads and settings, and its own taskbar button, title and color. The "Codex CLI in Terminal" button opens a terminal with that profile's own CODEX_HOME, so the command line is signed in to the same account as the window. It's $24.99 once, for two computers (Mac or PC), with a free 1-hour preview, and needs Windows 10 or 11 with the ChatGPT app from the Microsoft Store.
For the command line alone, though, everything above is free. One folder per account, one function or Terminal profile per folder, and you'll stop signing out to switch.
來源:DEV Community · dev.to