You can use my tool uhubctl, it supports Raspberry Pi models B+, 2B, 3B, 3B+ and 4B - these models have hardware ability to turn USB power off and on.
Use it like this:
Turn off power to all USB ports (must use port 2):
sudo uhubctl -l 1-1 -p 2 -a 0
Turn on power to all USB ports (must use port 2):
sudo uhubctl -l 1-1 -p 2 -a 1
Turn off power to Wifi+Ethernet (must use port 1):
sudo uhubctl -l 1-1 -p 1 -a 0
Note that Raspberry Pi 4B is very different from previous models as it has USB3 chip. It doesn't support turning off power to Wifi+Ethernet, and for USB you will need to use something like that to turn off (this turns off all USB ports - RPi 4B hardware does not support individual port power control):
sudo uhubctl -l 2 -a 0
If your Raspberry Pi does not support USB power switching, you can connect external USB hub that does (see list of compatible models), and control power on that external hub using uhubctl
.