WP-CLI is a powerful command-line tool for managing WordPress. Unfortunately, most of the information on the Internet on how to install it comes from a “Linux”-centric point of view. There are tutorials on how to install it on Windows, but all of the ones that I have seen make it sound complex or incompatible for users who may not generally use command-line tools or PowerShell.
Very simple tldr; version
If you don’t need a lot of help or details, here’s the basic breakdown of installing WP-CLI:
- Download the WP-CLI phar file
- Save it to a logical path, such as c:/wp-cli/wp-cli.phar
- Create a batch file as wp.bat in that directory with the following (change php.exe path to actual path to your PHP executable):
c:/path/to/php/php.exe "c:/wp-cli/wp-cli.phar" %*
- Go to System’s Properties, click Advanced System Properties
- Select the tab labeled Advanced, click Environment Variables
- Under the System variables section scroll down to select the variable named “Path” and click Edit
- Add ;C:\wp-cli to the list
Those are the simple steps. If you need a broad explanation, that is what follows.
Are there dependencies?
There are some, but not a lot. The one primary thing you need is for your machine to be running PHP.
If you run WordPress on a Windows host or your local development for WordPress is Windows, you already are running PHP somewhere.
WP-CLI is a PHP “phar” file that will run on your machine as long as you have PHP available to execute.
What’s a phar?
WP-CLI is packaged as a “phar” file. This is an entire PHP application compiled into an executable file that runs using PHP.
How to Install
Once you have downloaded it, move it to where you want to keep it. This example will assume a very simple path: c:/wp-cli
Open a Windows Command Prompt or the PowerShell and enter the following
php c:\wp-cli\wp-cli.phar --info
You should get a result that looks like this:
If that’s what you got, congratulations! You have installed WP-CLI successfully.
If you got an error about PHP not being a valid command, then you haven’t set up PHP to run from the command line. That’s OK, but it means you have to figure out where your PHP executable is on your machine. For example, if your PHP path is c:\php\php.exe, then you can use the following command to check your install:
c:\php\php.exe c:\wp-cli\wp-cli.phar --info
Once you know your PHP path (where your php.exe file is) and the WP-CLI path (where your wp-cli.phar file is), and you’ve check that with the –info parameter to verify it is working, you can move on to make entering commands much simpler than typing the full path every time you want to run a command.
Create the wp Command With a .bat File
Now let’s create a batch file so you can run your commands without typing the full path to the phar file.
Open your favorite editor. If you use an IDE, you can use that, but it can be as simple as Notepad. Nothing that you MUST have the valid path to your php.exe file, put the following in a file (changing the path to php.exe to whatever your actual path is):
@ECHO OFF c:/your/actual/path/to/php.exe "c:/wp-cli/wp-cli.phar" %*
Save your file as “wp.bat” in c:/wp-cli (make sure you don’t save it as a txt file – it should only have the .bat extension)
Now you can run your commands by simply typing “wp” followed by the command if you’re in the c:/wp-cli directory. But you’ll want to use the tool globally, since to actually use it, you need to be working from the folder of your WP install (which is not going to be c:/wp-cli). To do that, we’ll create an Environment Variable to link the “wp” command to our c:/wp-cli/wp.bat file.
Add the Command to the Path Environment Variable
This part depends a little bit on your system, but I’ll make the assumption that someone running Windows Server knows enough about this to follow the explanation below and translate to their system, or that they already know how to do this without it being explained. The examples below apply to a typical local environment (i.e. Windows 10 Home or such)
Go to the Windows icon on the task bar (the “start menu”), then click “Settings”. In the settings window, click System.
On the left menu, click “About” (see #2 in the image). Locate “Advanced system settings” and click that (#3 in the image).
In the System Properties, click the “Advanced” tag (#4) and then “Environment Variables” (#5).
With the Environment Variables settings open, in the User variables, select “Path” and “Edit”:
In the “Edit environment variable” window for path, click “New” and type in the path to the wp.bat file we made above (if following the example directly, then that is c:\wp-cli\wp.bat).
That’s it! Restart your command prompt window or PowerShell and you’re ready to use WP-CLI.
Basic commands for WP-CLI can be found here. Some plugins extend WP-CLI with custom commands for the plugin. Generally – at least when getting started – you’ll need to be running “wp” commands from the root directory (or below) of your WP install.