SQLServerCentral.com / Programming / Powershell / Problem copying .bak with "$" in file path/name (PS running in SQL job step) / Latest PostsInstantForum.NET v99.99.99SQLServerCentral.comhttp://www.sqlservercentral.com/Forums/notifications@sqlservercentral.comFri, 09 Dec 2016 08:56:28 GMT20RE: Problem copying .bak with "$" in file path/name (PS running in SQL job step)http://www.sqlservercentral.com/Forums/Topic1446248-1351-1.aspxYou are most welcome - makes a change for me being the helper as opposed to the helped!!!Fri, 26 Apr 2013 03:13:39 GMTGary VargaRE: Problem copying .bak with "$" in file path/name (PS running in SQL job step)http://www.sqlservercentral.com/Forums/Topic1446248-1351-1.aspxTHANKS for the info!!! It's very much appreciated.(like your sig... so in the same spirit, "Game over, man... GAME OVER!!!")...edit after reading ALL the examples... WHOA, that last one is slick as ... well, it's slick!Thu, 25 Apr 2013 12:57:04 GMTmstjeanRE: Problem copying .bak with "$" in file path/name (PS running in SQL job step)http://www.sqlservercentral.com/Forums/Topic1446248-1351-1.aspxWhen using double quotes (") PowerShell looks to replace variables with their values whereas if you use single quotes (') it does not. So as you used double quotes it interpreted $instname as a variable, for example.If you have no embedded variables then you can use the following:[code="other"]$from = '\\srvrname\r$\SQL\MSSQL10_50.instname\MSSQL\Backup\srvrname$instname\DBNAME\FULL\srvrname$instname_DBNAME_FULL_' + $dd + "*.BAK"[/code]Or you can escape the dollar signs ($) with a grave accent (`) like this:[code="other"]$from = "\\srvrname\r`$\SQL\MSSQL10_50.instname\MSSQL\Backup\srvrname`$instname\DBNAME\FULL\srvrname`$instname_DBNAME_FULL_" + $dd + "*.BAK"[/code]The following should work too:[code="other"]$from = "\\srvrname\r`$\SQL\MSSQL10_50.instname\MSSQL\Backup\srvrname`$instname\DBNAME\FULL\srvrname`$instname_DBNAME_FULL_$dd*.BAK"[/code]Hope this helps.Thu, 25 Apr 2013 03:48:58 GMTGary VargaRE: Problem copying .bak with "$" in file path/name (PS running in SQL job step)http://www.sqlservercentral.com/Forums/Topic1446248-1351-1.aspxWell... this fixed it. But now I'd like to know if anybody knows WHY it fixed it...[code="plain"]#this fails$from = "\\srvrname\r$\SQL\MSSQL10_50.instname\MSSQL\Backup\srvrname$instname\DBNAME\FULL\srvrname$instname_DBNAME_FULL_" + $dd + "*.BAK"#but this works$from = "\\srvrname\r$\SQL\MSSQL10_50.instname\MSSQL\Backup\srvrname" + "$" + "instname\DBNAME\FULL\srvrname" + "$" + "instname_DBNAME_FULL_" + $dd + "*.BAK"[/code]Wed, 24 Apr 2013 18:10:33 GMTmstjeanProblem copying .bak with "$" in file path/name (PS running in SQL job step)http://www.sqlservercentral.com/Forums/Topic1446248-1351-1.aspxI'm using OLA's SQL JOBs to do my database maintenance. So far, I really like them; they're very handy, especially when apps create new databases each month (WILDCARD dbnames!).I have to copy files across server instances on occasion; since we're deploying SQL2008r2 I've started using Powershell to do this.I've got a powershell job step that works fine to get bak files from my old servers to new servers.But now I have to copy a backup created by the Ola backup process... it includes $ in the path (as servername$instancename) and at the start of the backup file name.It LOOKS like the $ is confusing Powershell...Is there a control character I can use to allow the "$" into the path/filename, or is there [b]another [/b]way to do this?[code="plain"]#Example: (this is a powershell step in a sql job)# get yesterdays date as YYYYMMDD$dd = (get-date).AddDays(-1).ToString("yyyyMMdd")# here's the original source/target location for the backup file#$from = "\\srvrname\r$\SQL\MSSQL10_50.instname\MSSQL\Backup\srvrname$instname\DBNAME\FULL\srvrname$instname_DBNAME_FULL_" + $dd + "*.BAK"#$to = "\\SA01SQL002\u$\SQL\MSSQL10_50.TEST\MSSQL\Backup\from$instname\"#and here's the error on the copy-item line:# 'An object at the specified path \\srvrname\r$\SQL\MSSQL10_50.instname\MSSQL\Backup\srvrname\DBNAME\FULL does not exist.'# ********# whoops... that should say \srvrname$instname\## that blows up so tried wrapping $from and $to within a double quote... this didn't fly either.$from = """" + ...TheOriginalString... + """"$to = """" + ...TheOriginalString... + """"# the error references this line copy-item $from $to -Force -Recurse[/code]Wed, 24 Apr 2013 15:34:17 GMTmstjean