SQL Server Variables in Powershell

A variable is a symbolic name given to some known or unknown information, for the purpose of allowing the Variable name to be used independently of the information it represents. A variable name is usually associated with a data storage location and thus also its contents, and these may change during the course of program execution making Variables the most common & almost indispensable thing for a Developer in any scripting language.

In Powershell, variables always start with a $(Dollar) Sign. Variables can be Typed, but there is no Hard & Fast rule to it, you can opt to leave it untyped also.

Let’s practice couple of examples to understand it better.

Example1.  Text Variable

In this example we’ll see how to declare one variable in powershell and then concatenate a string to it without assigning a type to variable.

1_SQL_Server_Variables_in_Powershell

Example2. Int Variable 

In this example we’ll declare a untyped variable assign an int value and do calculation on the fly.

2_SQL_Server_Variables_in_Powershell

PS C:\>Example 3. Printing output to console with different colors

In this example, we’ll create 2 variables, assign some string value to them, create another variable and append the result of both earlier declared variables and then make use of Foregroundcolor and Backgroundcolor switches of Write-Host cmdlet to make it colorful.

   

3_SQL_Server_Variables_in_Powershell

4_SQL_Server_Variables_in_Powershell

eNjoy

 

Regards

Sarabpreet Anand

Like us on FaceBook Follow us on Twitter | Join the fastest growing SQL Server group on FaceBook

Follow me on Twitter  |  Follow me on FaceBook

   

Leave a Reply

Your email address will not be published.