When building a Powershell script with SMO to Backup/Restore databases I found an error when using folder names in the string. The following foldername sample will show you what’s correct (or not) when using the .AddDevice() method in either SQL Backup and Restore:
$Device1 = "C:\Temp\Sample.bak"
…AddDevice($Device1) #procedure will run
$Device2 = "C:\Program Files\Microsoft SQL Server\MSSQL\Backup\Nw.bak"
…AddDevice($Device2) #procedure will fail
$Device3 = "C:\’Program Files’\’Microsoft SQL Server’\MSSQL\Backup\Nw.bak"
…AddDevice($Device3) #procedure will run
If the folder name includes spaces then your SMO procedure will not work. You need to use the single quote in both beginning and end of the foldername string. Keep in mind, You don’t need to use the single quote in the string if you’re using the DOS Copy, CD, and MD commands. Always test you scripts!