Powershell – Handling Reserved Characters in a Password

February 16th, 2012 by Avi Moskovitz | 1 Comment | Filed in Microsoft, PowerShell

Recently I was using PowerShell to update records in a SQL database.  Unfortunately for me, the SQL credentials contained a dollar sign ($); which is a reserved character in PowerShell. 

Originally, building and sending my connection string (including the password within the string as usual) resulted in a failure to log in.  Even after some inclination that the dollar sign might be problematic, and encapsulating it in single quotation marks within the connection string, the login still failed.

After confirming that the connection string was indeed valid (through other means), the trick that ultimately worked in PowerShell was to set the password to a variable as a literal string (meaning, wrapped in single quotes) and then pass the variable along with rest of the connection string.

 

$psw = 'dbp$$wd'
$sConnString = "Driver={SQL Server};Server=10.0.0.1;Database=DBName;Uid=db_user;Pwd=$psw;"

 

This approach will apply to other special characters as well. 

Good luck!

 

 

Did you like this? Share it:

Tags: , , , , , , ,