
Dec 04, 2013
Quoting in PowerShell and embedding strings of text or evaluating expressions and variables in cmdlets are one of the main things that tend to trip up most newbies. Windows PowerShell was first released to the world around the time of Microsoft Windows Server 2008 and was designed from the outset as the next generation platform for administrative automation. If you want to become a server administrator these days, you do need to become familiar with PowerShell. PowerShell is a very versatile and powerful administrative scripting language that can do a lot more in one line than previous systems such as MS-DOS Command shell, Windows Scripting Host (WSH), and jScript; and more than VBScript could ever do in many paragraphs of code. Even though it is generally very forgiving about things such as spaces, PowerShell still has a clearly defined syntax. Before understanding quoting in PowerShell, we need to be clear that there are two quote characters that can be used, and they are both subtly different:- The single quote (the ‘ ' character (ASCII 39 in decimal), generally to the left of the Enter key on your keyboard). This is referred to as a literal quote because it takes the contents of your enclosed string in literal form
- The second quote type is the double quote (the “ " character (ASCII 34 in decimal), again generally found to the left of the Enter key, but this time used in conjunction with the Shift key). This quote is referred to as an expanding string quote mark (i.e. it can “expand” or evaluate expressions and variables in the string).
Echo ‘The World is Round’
OrEcho “The World is Round”
Both of the above will give the same output as picture below. However, if I wanted to evaluate a variable within a string, as written in the quotes below, the results would be quite different.$Days = 7 Echo ‘There are $Days in the week’
OrEcho “There are $Days in the week”
See how the first example takes the string literally, but the second example evaluates the enclosed variable. Now, that's cool! I did say earlier that we should not mix the quote marks, but we can combine them in some very interesting ways. Let’s say I wanted to include the double quotes within my string like this:Echo “My name is “Gordon””
You would think the above example makes logical sense, but it does give a strange output! However, if I combine the two types of quote marks in the following way, it produces a more readable display.Echo ‘My Name is “Gordon”’
So there you go, a couple of very useful tips for quoting in PowerShell. Now it's your turn to try out your own variations!How do your Excel skills stack up?
Test NowNext up:
- A Knotty Issue
- Deploying Web and Service Applications to Windows Azure
- Customising Layouts in SharePoint Designer 2010
- The Pros and Cons of SSAS Tabular Models in SQL Server 2012
- Manipulate dates with custom number formats in Excel 2010
- PowerPivot's CALCULATE function to the rescue
- The impact of discounting and why you should not be offering it
- High Availability in Lync Server 2013
- The truth about Santa Claus
- Happy New Year!
Previously
- How to integrate OneNote and Outlook
- Support the Salvation Army Christmas Appeal
- What's new in Windows 8.1 for Developers
- Centre objects in Photoshop CS6 with precision
- The dirty little secret of Transparent Data Encryption (SQL Server 2012)
- What's New in Lync Server 2013
- Quickly jazz up your Excel comments
- Prevent Black and White Documents Separating to CMYK in InDesign
- Why you should get a Microsoft Certification today
- Understanding Airbrush in Photoshop