This post demos a script I put together to help move SQL Server database files to another drive and folder by generating PowerShell and T-SQL commands.
I’ve decided to make this script for situations where installing the dbatools PowerShell module wouldn’t be possible.
Otherwise, I highly recommend using dbatools’ Move-DbaDbFile command.
Click through for the script, and I second Vlad’s recommendation of dbatools for this kind of effort.