Avoiding the dollar sign in powershell path doesn't work
Why is this not working?
$drvrInstFilePath = "$sharePath\$imageName\ISO`$OEM$`$1\RPKTools\RPKDriverInst.bat" echo $drvrInstFilePath $drvrInstContent = Get-Content -LiteralPath "$sharePath\$imageName\ISO`$OEM$`$1\RPKTools\RPKDriverInst.bat" | Out-String The echo shows the correct path, but does the Get-Content command expand $ oem and $ 1 to empty lines, even if they are escaped?
Instead of messing around with winning dollar signs, use single quotes ' instead of double quotes. " This prevents Powershell $ from expanding into a variable. Thus,
$p = "C:\temp\Share\ISO$OEM$" # Output C:\temp\Share\ISO$ $p = 'C:\temp\Share\ISO$OEM$' # Output C:\temp\Share\ISO$OEM$ If you need to create a path using variables, consider using Join-Path . Thus,
$s = "share" join-path "c:\temp\$share" 'Share\ISO$OEM$' # Output c:\temp\Share\ISO$OEM$ You used double quotes with one back. This is the wrong combination. In fact, Iām not sure that in any case, one return stroke is enough. Your successful options for avoiding the dollar sign ($) in PowerShell are to use double quotes with a combination of backslash-backtick ("\` $ find ") or instead use single quotes with a simple backslash ('\ $ find') . [However, note the exception at the end about function call parameters.] The following are examples.
In addition, for those who are not familiar with the difference, it is important not to confuse the backtick character ( `) with the single quote character (') in these screens.
[SUCCESS] Double quotes as a container with a backslash-backward step as an escape code:
PS C:\Temp> 'What is $old?' | ForEach-Object {$_ -replace "\`$old", "(New)"} What is (New)? PS C:\Temp> [FAIL] Double quotes as a container with a backslash apostrophe as escape:
PS C:\Temp> 'What is $old?' | ForEach-Object {$_ -replace "\'$old", "(New)"} What is $old? PS C:\Temp> [SUCCESS] Single quotes as a container with a simple backslash as an escape:
PS C:\Temp> 'What is $old?' | ForEach-Object {$_ -replace '\$old', "(New)"} What is (New)? PS C:\Temp> [FAIL] Single quotes as a container with backslash as output:
PS C:\Temp> 'What is $old?' | ForEach-Object {$_ -replace '\`$old', "(New)"} What is $old? PS C:\Temp> In general, the simplest option would be to use single quotes as a container and one backslash as escape: '\$old'
Refresh . As if the above were not complex enough when you use a function call instead of a command, you need single quotes without output. Trying to use escape on a function call parameter will not work:
[FAIL] Using an escape function in a function parameter:
PS C:\Temp> 'What is $old?' | ForEach-Object {$_.ToString().Replace('\$old', "(New)");} What is $old? PS C:\Temp> [SUCCESS] Using simple single quotes without an escape function in a function parameter:
PS C:\Temp> 'What is $old?' | ForEach-Object {$_.ToString().Replace('$old', "(New)");} What is (New)? PS C:\Temp> In my case, I needed to avoid using the $ used in the string, but not other variables.
For example, my SSRS instance name has a $ sign:
[ReportServer $ SSRS]
To avoid the $ sign, I use single quotes. Otherwise, I use -join to combine variables with strings containing valid $ characters.
$sql = -join('ALTER DATABASE [ReportServer$', $instanceName,'TempDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GO USE [master] RESTORE DATABASE [ReportServer$', $instanceName,'TempDB] FROM DISK = N''C:\temp\ReportServerTempDB.BAK'' WITH FILE = 1, MOVE N''ReportServerTempDB'' TO N''', $sqlDataDrive + "\data_" + $instanceName, '\ReportServer$', $instanceName,'TempDB.mdf'', MOVE N''ReportServerTempDB_log'' TO N''', $sqlLogDrive + "\log_" + $instanceName, '\ReportServer$', $instanceName,'_TempDBlog.LDF'', NOUNLOAD, REPLACE, RECOVERY, STATS = 5 GO ALTER DATABASE [ReportServer$', $instanceName,'TempDB] SET MULTI_USER; GO ')