error 5(Access is denied.) while attempting to open or create the physical file error 5(Access is denied.) while attempting to open or create the physical file

error 5(Access is denied.) while attempting to open or create the physical file

This error can occur when using SQL Change Automation Powershell, with the default option of validating the schema folder in LocalDB, and you have LocalDB 2017 installed. 

This is caused by a bug in LocalDB 2017 so you will need to install the latest Microsoft Update for LocalDB 2017:

https://www.microsoft.com/en-us/download/details.aspx?id=56128

An alternative workaround if updating LocalDB is not possible straight away, is to use the TemporaryDatabase or TemporaryDatabaseServer on the schema validation step (Invoke-DatabaseBuild), since setting either one of these will cause the tool to do the schema validation in a SQL Server instance rather than in LocalDB.