Skip to content

Instantly share code, notes, and snippets.

@joegasper
Created January 21, 2023 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joegasper/511907a05fa0cfedab9fe558be5a5f64 to your computer and use it in GitHub Desktop.
Save joegasper/511907a05fa0cfedab9fe558be5a5f64 to your computer and use it in GitHub Desktop.
WordPress WP-CLI Installation on Windows

WordPress WP-CLI Installation on Windows

Prerequisites: Chocolatey, PHP, Composer

Use an elevated PowerShell console

Install chocolatey

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

Set choco to remember installation options when upgrading

choco feature enable -n=useRememberedArgumentsForUpgrades

Install latest PHP to a set path (choco will add the path to the path environment variable)

New-Item -Path D:\ -Name 'PHPsys' -ItemType Directory
choco install php --params "/InstallDir:D:\PHPsys"

Start a new elevated PowerShell console (for the updated $env:path)

Install composer (a PHP package manager) and use the installed PHP

choco install composer --params "/Php:D:\PHPsys"

Edit D:\PHPsys\php.ini and uncomment the lines for needed/useful extensions

(Get-Content -Path D:\PHPsys\php.ini) -replace ';extension=fileinfo','extension=fileinfo' | Set-Content -Path D:\PHPsys\php.ini
(Get-Content -Path D:\PHPsys\php.ini) -replace ';extension=gd','extension=gd' | Set-Content -Path D:\PHPsys\php.ini
(Get-Content -Path D:\PHPsys\php.ini) -replace ';extension=exif','extension=exif' | Set-Content -Path D:\PHPsys\php.ini
(Get-Content -Path D:\PHPsys\php.ini) -replace ';extension=mysqli','extension=mysqli' | Set-Content -Path D:\PHPsys\php.ini
(Get-Content -Path D:\PHPsys\php.ini) -replace ';extension=pdo_mysql','extension=pdo_mysql' | Set-Content -Path D:\PHPsys\php.ini
(Get-Content -Path D:\PHPsys\php.ini) -replace ';extension=zip','extension=zip' | Set-Content -Path D:\PHPsys\php.ini

Start a new elevated PowerShell console (for the updated $env:path)

Install wp-cli and suggested/required complementary packages - use of 'global' places the packages in a path already in $env:path

composer.bat global require wp-cli/wp-cli-bundle 
composer.bat global require psy/psysh 
composer.bat global require ext-zip 
composer.bat global require wp-cli/doctor-command

You may get an install conflict on wp-cli/wp-cli install about wp/wp.bat - fixed with next step.

Update $env:USERPROFILE\AppData\Roaming\Composer\vendor\bin\wp.bat to work on Windows

((Get-Content -Path "$env:USERPROFILE\AppData\Roaming\Composer\vendor\bin\wp.bat") -replace 'bin/wp','bin/wp.bat') -replace 'sh ','' | Set-Content -Path "$env:USERPROFILE\AppData\Roaming\Composer\vendor\bin\wp.bat"

Test the installation

wp --info

OS:     Windows NT 10.0 build 20348 (Windows Server 2022) AMD64
Shell:  C:\WINDOWS\system32\cmd.exe
PHP binary:     D:\PHPsys\php.exe
PHP version:    8.2.1
php.ini used:   D:\PHPsys\php.ini
MySQL binary:
MySQL version:
SQL modes:
WP-CLI root dir:        C:\Users\myaccount\AppData\Roaming\Composer\vendor\wp-cli\wp-cli
WP-CLI vendor dir:      C:\Users\myaccount\AppData\Roaming\Composer\vendor
WP_CLI phar path:
WP-CLI packages dir:
WP-CLI cache dir:       C:\Users\myaccount/.wp-cli/cache
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment