Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



Windows PowerShell Quotes

Introduction to Windows PowerShell Quotes

Did you ever use a traditional single-bladed screwdriver on a square 'Philips' head?  Sometimes you get away with it, but sooner or later you will literally "screw up" - well it's the same with PowerShell's quotes.

Topics for PowerShell Quotes

 ♣

Introduction to the right sort of PowerShell quotation mark

In Windows PowerShell, there are two types of quotes 'single speech marks' and "double speech marks"; each has a particular meaning and significance.  The first point is that merely because you use a string, PowerShell does not require any sort of quote.  That said, there are times when you will need not only quotes, but also the right sort of quote, for example:

# PowerShell quotes needed
set-location c:\ documents and settings. 

The above command draws an error message, something about a parameter called and.  Fortunately, quotes will solve the problem thus:

set-location "c:\ documents and settings"
# or
set-location 'c:\ documents and settings'

Either single or double quotes work in this instance, and you may deduce that they will always be interchangeable - wrong.  Take the time to understand the different role for 'single quotes' and "double quotes".

Example 1 PowerShell 'Single Quote'

Call me paranoid, but when I say 'Single Quote', I am talking about the key sandwiched between the semi-colon and the hash # key.   This key is also known as the Apostrophe and corresponds to ASCII 039 in decimal.

# PowerShell Single Quotes
$Days = 7
Write-Host 'There are $Days in a week'

We have a problem, the above command does not expand the variable $Days into the number 7.  What happens instead is we get the literal: There are $Days in a week.   Let us see what happens with double quotes in example 2.

Guy Recommends:  SolarWinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click to import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

Example 2 PowerShell "Double Quotes"

Double quotes are required in situations where you need to expand variables.  This is also known as variable substitution, and it only happens if you employ double quotes.

# Note: "Double quotes for $Days"
$Days = 7
Write-Host "There are $Days in a week"

In Example 2 PowerShell expands the command, it switches to what I call mega clever mode, and realizes that I intended $Days to be a variable and not a literal word.

While the "double quote" is a distinctive symbol, its location on the keyboard varies according to your country.  However, if you are in doubt then check what I mean by holding down the left hand Alt key, and typing 034 on the numeric keyboard.  What makes me twitchy about the precise definition of the single and double quotes is the memory of spending 3 days troubleshooting a script where a 'Psycho' used two single quotes instead of one double quotes.  It took the eyes of a hawk to distinguish between "Results" and ''Results''.

Summary of PowerShell Quotes

PowerShell employs both single and double quotes, however they have different purposes.  Double quotes expand the enclosed string, whereas single quotes treat the string as a literal.  What ever you do, avoid the trap of two single quotes (ASCII decimal 039) when you need a double quote (ASCII decimal 034).

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials

PowerShell Tutorials  • Methods  • Cmdlets  • PS Snapin  • Profile.ps1  • Exchange 2007

Command & Expression Mode  • PowerShell pipeline (|)  • PowerShell 'where'  • PowerShell 'Sort'

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, SolarWinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: