SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Powershell in a Month Day 18–Variables

This is part of my Powershell Challenge, to learn more about PowerShell (PoSh) using the Learn Windows Powershell 3 in a Month of Lunches book by Don Jones.

I’m slightly behind here after holidays and some travel. Apparently my pop-up reminder for my own studies bears about as much value as quite a few other reminders that appear from people at work.

I’ve had to deal with variables in any number of languages in my career: BASIC, Pascal, C, C++, Java, C#, VB6, VB.NET, Foxpro, T-SQL, and a few more. I thought this chapter would be simple and it certainly started out that way, but I learned a few things and was pleasantly surprised.

NOTE: The editor here is substituting left and right quotes, and left and right double quotes as part of formal writing. In executing this code, the normal straight quote and double quotes are needed in PoSh.

The chapter starts out with the simple stuff, assigning values to variables. Unlike most languages,we don’t need to declare variables. You need one, just use it.

$domain = ‘SSCLab’

I’ve done this quite a bit in some of the practice for this series, but also in setting up my virtual lab. However I wasn’t sure why single and double quotes matter. If you’re not sure, this this:

$computer = ‘Tiny’

$Phrase = ‘Computer: $computer’


and then this

$computer = ‘Tiny’

$Phrase = “Computer: $computer”


You’ll see a difference. The first produces

Computer: $computer

The second actually does substitution and produces

Computer: Tiny

That means that you really need to pay attention to which set of quotes you are using in code. That’s an area I need to be careful. I also learned about the backtick as an escape character, but that’s an easy one as it’s similar in lots of languages to escape things out for newline and other special characters.

What’s interesting about PoSh variables, and probably one of the more powerful things, is that the variables can contain objects. That’s big. It’s like being able to overload classes into variables easily and quickly. I’m sure the exercises just scratch the surface, but it’s cool.

The lab was easy, just getting a job running and putting results in a variable. A combination of a few of the last few chapters.

Filed under: Blog Tagged: career, powershell, syndicated

The Voice of the DBA

Steve Jones is the editor of SQLServerCentral.com and visits a wide variety of data related topics in his daily editorial. Steve has spent years working as a DBA and general purpose Windows administrator, primarily working with SQL Server since it was ported from Sybase in 1990. You can follow Steve on Twitter at twitter.com/way0utwest


Leave a comment on the original post [voiceofthedba.wordpress.com, opens in a new window]

Loading comments...