Getting WP CLI Shell To Work On Windows

The WP CLI shell feature is a great way to test out WordPress code in a playground / sandbox type environment, but it doesn’t work out of the box on Windows. If you try running it in Windows you’ll get something like

wp shell
Error: The shell binary '/bin/bash' is not valid. You can override the shell to be used through the WP_CLI_CUSTOM_SHELL environment variable.

So let’s get that going. The first thing to know is that Windows uses .bat files to allow shortcuts for typing commands instead of typing the full path in command line.

Pre) If you haven’t already, you should have added your PHP path to windows Environment Variables, this can be done following the below steps, but add a second Path to where you have your Windows PHP bin example: C:\WTServer\bin (I’m using https://winnmp.wtriple.com/ which I highly recommend for a Windows PHP stack using Nginx because it’s much faster than WAMP). If you have done this then when you type in command line: php -v you should see your php version show.

Step 1) Download this repo to your computer: https://github.com/lipemat/wp-cli-bundle this is a fork of the regular WP CLI but with a fix for psysh for Windows so that WP CLI works (thank you Mat Lipe!)

Step 2) Go to the root of where you downloaded this repo and run: composer install in command line

Step 3) This will build out wp-cli.phar into the vendor/wp-cli/wp-cli/bin inside this directory is a wp.bat file which can be used by your windows environment path. Delete all the other .bat files (this is so that you don’t have any interfering commands for other ones like composer) so the directory will only have wp and wp.bat in it. Add to your windows environment path this directory so that you can use wp in command line to access wp-cli

This can be done by searching in Windows for: Environment: and going to “Environment Variables” > Path > Edit > New and add the full path to where you have it for vendor/wp-cli/wp-cli/bin IE “C:\Users\(your username)\Downloads\wp-cli-bundle-master\wp-cli-bundle-master\vendor\wp-cli\wp-cli\bin\wp”

Now we should be able to test wp shell, try typing that in command line (make sure to close the current command line so that Windows reloads the paths first) from within your WordPress installation directory, and after that use a simple command like get_bloginfo( 'name' );

If it’s working you should see the output of the name of your WordPress site.