Settings up Drush on a Windows PC

The problem with Drush on Windows

Windows just isn't geared up to work with Drush out the box for things like site alises, sql-sync, rsync etc.

The Solution

Site aliases

Site alias commands involving SSH will fail because drush will use single quotes rather than double quotes. To solve this, you just need to add a os setting to the drush alias file like this: -

site:
  os: Windows

That's it. Now when the command is concatenated, it will know you are using Windows and use the double-quotes.

Next you'll need to add your local site alias if you plan to copy files or databases between sites. Documentation can found all over to do this. You'll need to put these into drush/sites/example.site.yml in your project's docroot, or you can put them in your home directory's site alias directory.

drush rsync on Windows

You will need to download and extract cwRsync from here - https://www.itefix.net/cwrsync. Now it doesn't work out the box. You'll have to add the path of wherever you extracted to into the windows path variables.

Next problem is (depending on your command line interpreter) rsync will see paths like c:/folder as a remote destination, so expect errors like this: -

"the source and destination cannot both be remote"

To resolve this problem, create a rsync.bat file in bin directory of wherever you extracted cwrsync to and put something like this into it: -

@echo off
SETLOCAL
SET CWRSYNCHOME=C:\cwrsync
SET HOME=c:\Users\<your user>\
SET CWOLDPATH=%PATH%
SET PATH=%CWRSYNCHOME%\bin;%PATH%
SET args=%*
set newargs=%args:C:/=/cygdrive/c/%
set newargs=%newargs:D:/=/cygdrive/d/%
rsync.exe %newargs%

This simply replaces C:\ with a path that cwrsync recognises as a local path.

Now you should be able to rsync from and to your site's docroot like this: -

drush rsync @self:sites/default/files @<site-alias>:sites/default/files

drush sql-sync

Lastly to ensure sql-sync works you need to add the mysql path to your windows environmental variables. If you see any errors with rsych, refer to the section above to get that working first.

Conclusion

That's it I think. Reach out if you have any questions or problems getting drush to work in Windows.

P.S. It's always useful to know what's going on. Running drush with a -vvv argument dumps all sorts of useful info which can help debug and fix any issues you may be having.

Get in touch

This form collects your name, telephone number (optionally) and email address only so we can contact you back about your enquiry. We do not and will never use your data for any other purposes other than contacting you regarding this enquiry and no data is shared with any third-party.