Sometimes you detach databases, or you delete a database which is in restoring status, the data and log file will not be removed from local disk. if you forget the files, It might cause issues
1. The files waste disk space
2. If you restore the database back to the original place, the restore will be failed because of the existing files.
so here is a powershell script which can find out orphan database files on disk. the script can
1. Get the database default file path from registry
2. Search all mdf, ndf and ldf files
3. Check the sql server system table to see if all files are attached.
4. Print out the file name if the file is not attached to any database.
you can download the script from here
and run the script as below:
here, 2 orphan db files(my.mdf and my.ldf) are found in instance sqlexpress.