PowerShell tutorial part 4

Almost all of your PowerShell statements are going to include string values. Most of the time these strings are simply passed to cmdlets as arguments, but in a few cases, those strings are enclosed in either double or single quotes. The rules that govern when to use single or double quotes are referred to as quoting rules. This tutorial will attempt to explain those rules.

When text is enclosed in quotes PowerShell treats that text as a string. As long as you are not using any special characters or reference variables, strings can be enclosed in either single or double quotes.

write-output "the quick brown fox"
write-output 'the quick brown fox'

In both of these examples the Write-Output cmdlet sends the string output directly to the powershell console. The output of both strings appear identical. In addition to the Write-Output cmdlet, PowerShell’s Out-host and Write-Host cmdlets send information to the console. Write-Output sends output down the pipe to the next cmdlet. When Write-Output is the last cmdlet in the pipeline the output is displayed in the console. The Out-Host cmdlet sends output directly to the console but offers an optional parameter that lets you view the output one screen at a time. Write-Host on the other hand has two optional parameters that let you change the color of the text or background, allowing for a custom console. All three though behave in similar ways.

write-output "the quick brown fox"
write-host "the quick brown fox"

the out-host cmdlet is a bit different;

a$ = get-history
out-host -inputobject $a

to explain a bit about the out-host example. First we define a variable a$ and set it equal to the get-history cmdlet. Then we use out-host to call the results of the get-history cmdlet by using it as input with the -inputobject $a.

If you want to include quotes inside a string you can use single quotes either within double quotes or double quotes within single quotes.

write-output "the quick 'brown' fox"
write-output 'the quick "brown" fox'

when you use the same quotes inside the string though you get different results for example try the following.

write-output 'the quick 'brown' fox'
write-output "the quick "brown" fox"

the reason the output is different is because PowerShell interprets the one string as multiple strings and as a result adds a line break. Be careful not to mix up the types of quotes or forget to include a quote because doing so might result in you getting stuck in a loop that continues to prompt you for an entry. If you do get stuck in such a loop pressing CTRL + C will break the loop and return you to a prompt. Another thing to be aware of is when defining cmdlet arguments for numerical values. Type and run the following

Write-output "123"

As you can see the output is the same as the earlier examples. Now lets try another example

(write-output "123").GetType()

This statement uses the Gettype method of the object’s type. In this case the object type is a System.String. If you do not enclose a numerical value in quotes, then PowerShell will treat that value as a numerical object.

write-output 123
(write-output 123).gettype()

the output at first looks identical. But when we ask powershell for its type (gettype) we see that now the value 123 is Int32 . If a value includes both letters and numbers then PowerShell treats it as a string whether it’s in quotes or not.

write-output 123four
(write-output 123four).gettype()

Once again we see that the value is a string.

Most of the time you can omit using quotes if your argument is a string without embedded spaces, take for example the following.

set-location c:
set-location "c:"
set-location 'c:'

now lets try;

set-location c:documents and settings

this statement generates an error due to it not knowing what to do with the words after the first space. For this to work we need to enclose the entire argument in quotes.

set-location "c:documents and settings"

Another important issue when working with strings is how to reference variables within a quoted string. If you enclose a string in double quotes, the variables value is used, but if you enclose the string in single quotes, the literal value is used.

Single quotes always treat a string literally, however double quotes allow you to escape special characters within the text. A special character is one that, when preceded by a back tick (`) takes a specific action that it would not have taken without. Here are a list of special characters.

  • `O – inserts a null value
  • `a sends an alert (bell or beep) to the computers speaker
  • `b inserts a backspace
  • `f inserts a form feed
  • `n inserts a new line
  • `r inserts a carriage return
  • `t inserts a horizontal tab
  • `v inserts a vertical tab
  • `’ inserts a single quote
  • `” inserts a double quote

to illustrate the difference between single and double quoted strings take a look at the following.

write-output '`tindented`n`twords'
write-output "`tindented`n`twords"

Leave a Reply