Salesforce CLI

The CLI works, Claude can reach your sandbox, and then a client asks you to “also look at UAT.” Adding another org is one command and a sensible nickname, on a Mac or a PC. Here is the whole thing, plus the cleanup you will need later.

One commandsf org login web does the whole job
About five minutesPer org, and most of that is logging in
Mac or WindowsThe same commands in Terminal or Command Prompt
Nothing to install in SalesforceThe login is saved on your computer
On this page
  1. The problem
  2. Mac or Windows
  3. 1 · Pick a nickname
  4. 2 · Log in
  5. 3 · Check it worked
  6. Housekeeping
  7. Cheat sheet
  8. Gotchas and FAQ
The problem

You set up one org and forgot how.

You got the Salesforce CLI working, felt like a hacker for a solid afternoon, and moved on with your life. Now there is a second sandbox, and the first setup is a blur.

Good news: adding an org is much smaller than the first setup. Nothing gets installed. You log in once, give the org a nickname, and from then on the CLI (and Claude, when you ask it to use the CLI) knows which org you mean.

This is the quick version, for when the CLI is already installed. If you are starting from zero, do the Salesforce CLI setup guide first and come back.

Before you start

Same commands on a Mac or a PC.

Every sf command in this post is identical on both. What changes is where you type it, and how you write a folder path.

 MacWindows
Where you typeTerminalCommand Prompt (not PowerShell)
How to open itCommand + Space, type Terminal, press ReturnClick Start, type cmd, open Command Prompt
Your home folder~%USERPROFILE%
Folder separator/\
Go to a client foldercd ~/salesforce-cli-projects/acmecd %USERPROFILE%\salesforce-cli-projects\acme

Why not PowerShell?

PowerShell can block some of these commands with a “running scripts is disabled on this system” error, which is a fun thing to debug ten minutes before a client call. Command Prompt does not have that problem. If you do see that error, you are in PowerShell. Open Command Prompt and run the same command again.

Only one command in this post needs a folder path (the default org one in Housekeeping), and it is written out both ways. Everything else you can paste as is.

Step 1

Pick a boring nickname.

Every org you log in to gets a nickname, which the CLI calls an alias. You will type it constantly, so make it obvious.

Use client-environment: lowercase, dashes, no spaces. Claude uses the same nickname to work out which org you mean, so a clear name saves you from deploying to the wrong place.

  • acme-dev, acme-uat, acme-partial for a client’s sandboxes
  • acme-prod for their production org, which gets its own lecture in Step 3
  • my-dev-org for your own Developer Edition playground

Future you, at 4:45 on a Friday, will be grateful there is no org called sandbox2-new-FINAL.

Step 2

Log in with the right address.

One command. The only part that changes from org to org is the login address you give it.

Open Terminal or Command Prompt. It doesn’t matter which folder you are in, because the CLI saves logins for your whole computer. Pick the address for the kind of org you are adding:

Adding aUse this --instance-url
Sandboxhttps://test.salesforce.com
Production or Developer Edition orghttps://login.salesforce.com
Org with single sign-on or a custom login pageThat org’s My Domain address, such as https://acme--uat.sandbox.my.salesforce.com

Adding Acme’s UAT sandbox looks like this, on either computer:

sf org login web --alias acme-uat --instance-url https://test.salesforce.com
  1. A browser window opens on the Salesforce login page. Log in with that org’s username. Sandbox usernames usually end in the sandbox name, like you@acme.com.uat.
  2. Click Allow when Salesforce asks to grant access.
  3. Go back to the terminal and wait for the success message.

What you should see: Successfully authorized you@acme.com.uat with org ID 00D…

The most common mistake here is logging in to the wrong org, and it usually isn’t your fault. The browser happily reuses whatever Salesforce session it already has open. Log out of Salesforce in that browser (or use a private window) and run the command again with the same alias. The new login replaces the wrong one.

Step 3

Check that it is really there.

Two commands: one to see the list, one to open the org and confirm it is the one you meant.

sf org list

Your new alias should be in the table next to its username. To be extra sure, open it straight from the CLI:

sf org open --target-org acme-uat

If Setup opens in the org you expected, you are done.

The production lecture

You can log the CLI in to production, and sometimes you need to. Once you do, every command that names acme-prod runs for real, in the client’s live org, with no undo button. End the nickname in -prod so it jumps out of sf org list, double-check the org name before you run anything that changes something, and only log in to production when you actually need it. Build in the sandbox, test in the sandbox, deploy like a grown-up.

Housekeeping

The cleanup you will need eventually.

Sandboxes get refreshed, nicknames turn out badly, and engagements end. Each one is a single command.

REFRESH

The sandbox got refreshed and now nothing works?

A refreshed sandbox is technically a brand-new org with a new org ID, so your old login stops working. Run the same login command from Step 2 again, with the same alias. Everything that used that nickname works again, and you don’t have to change anything else.

RENAME

You named something badly?

Point a new alias at the same username, then delete the old one. The username is in the sf org list table.

sf alias set acme-uat=you@acme.com.uat
sf alias unset sandbox2-new-FINAL
DEFAULT ORG

Tired of typing --target-org?

Set a default org for one client’s project folder (a folder made with sf project generate). Go to that folder first, and the default only applies there.

Mac (Terminal):

cd ~/salesforce-cli-projects/acme
sf config set target-org=acme-dev

Windows (Command Prompt):

cd %USERPROFILE%\salesforce-cli-projects\acme
sf config set target-org=acme-dev

I would still name the org in every request to Claude. “Deploy to acme-uat” is a lot harder to misread than “deploy it.”

OFFBOARDING

The engagement ended? Log out of the org.

Run sf org logout with the org’s nickname:

sf org logout --target-org acme-uat

The CLI asks if you are sure, and the answer defaults to No, so type y and press Return. Clean offboarding is a love language.

Mac Terminal showing the command sf org logout --target-org abc-dev and the prompt Are you sure you want to log out of, followed by a pixelated username and (y/N). An earlier Successfully authorized line above it also has the username and org ID pixelated.
The capital N in (y/N) means pressing Return on its own cancels. You have to mean it.

Run sf org list afterwards to check the nickname is gone. To log out of every org at once, use sf org logout --all.

Cheat sheet

Every command on one screen.

Swap in your own nickname and username. These are the same on a Mac and on Windows.

I want toCommand
Add a sandboxsf org login web --alias client-env --instance-url https://test.salesforce.com
Add production or a Developer Edition orgsf org login web --alias client-prod --instance-url https://login.salesforce.com
See every org I’ve addedsf org list
Open an org in the browsersf org open --target-org client-env
Rename an aliassf alias set new-name=username, then sf alias unset old-name
Set a default org for a foldersf config set target-org=client-env (run it inside the project folder)
Remove an orgsf org logout --target-org client-env
Remove every orgsf org logout --all
Gotchas

The things that go wrong, and the fixes.

It logged me in to the wrong org
The browser reused a Salesforce session that was already open. Log out of Salesforce in that browser, or use a private window, then run the login command again with the same alias. The new login replaces the old one.
command not found: sf, or ‘sf’ is not recognized
Close the terminal window and open a new one, because a terminal only notices programs that were installed before it opened. If it still fails, the CLI isn’t installed on this computer. The setup guide covers the install.
Windows says running scripts is disabled on this system
You are in PowerShell. Open Command Prompt instead (Start, type cmd) and run the same command.
No browser window opened
Look in the terminal for a web address. Copy it into your browser and log in there. If the login page is the wrong one, check that you used the right --instance-url for that kind of org, or the org’s own My Domain address if it uses single sign-on.
The terminal just sits there after I log in
Quit the terminal (Command + Q on a Mac, or close the window on Windows), open a new one and run sf org list. If the nickname is there, the login worked. If it isn’t, run the login command again.
A command says it can’t find my org
The nickname has to match sf org list exactly, dashes and all. If it does match, the login has probably expired, often because the sandbox was refreshed. Run the login command again with the same alias.

Using the Salesforce-hosted connector in Claude instead of the CLI? That is a different setup with its own steps: connecting multiple orgs through the Claude Salesforce connector.

Keep it handy

Bookmark the cheat sheet.

You will add an org maybe twice a year, which is exactly often enough to forget the command every time.

Know someone who just got handed a second sandbox?

Share on LinkedIn