Powershell - single quote, double quotes and protection

To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make a donation. Thank you!

Hello,

An article to review single quote ’, double quote ” string values and protections.

The PowerShell demo code

# Get the 1st service.
$service = Get-Service | select-object -first 1
# or
$service = (Get-Service)[0]
# Retrieve its name. I'm using this syntax deliberately for the demonstration.
$name = $service.name
# Let's test the following syntaxes:
'The name of the service is $name'
The name of the service is $name
# Note that the simple quote does not interpret variables
# Simple quote processing is faster,
# Powershell doesn't have to worry about variable interpretation.
# Was the $name variable essential?
"The name of the service is $service.name"
'The name of the service is $service.name'
# The simple quote remains true to itself
# The object.property principle does not work with "" quotes,
# In this case it displays the typename_of_the_object.name.
# You need to use the $() syntax for this to work (except in simple quote).
# $() is a sub-expression, the content is evaluated and the result sent:
"The service name is $($service.name)"
'The name of the service is $($service.name)'
# Personally, I prefer to use the -f format operator, which is also recommended.
The service name is {0}' -f $service.name

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-5.1

# To protect a character, use in this context `,
# in other contexts (operators...), it's \ :
"The service name is `$name"
# How to pass a non-Powershell command with characters
# characters that make sense in Powershell, such as (){} $
# Let's test the following command, which defines the delay during which the OS choice menu
# menu is available during a multiboot (the default value is already 30 seconds):
bcdedit /set {bootmgr} timeout 30
# To solve the problem
# Protect each character that causes trouble:
bcdedit /set `{bootmgr`} timeout 30
# or use --% (stop-parsing token) which tells Powershell to treat
# all the following characters as simple characters:
bcdedit --% /set {bootmgr} timeout 30
# But there may be a problem, for example :
$time = 30
bcdedit --% /set {bootmgr} timeout $time
# The variable is no longer interpreted, so use protections in this context:
bcdedit /set `{bootmgr`} timeout $time
# Why do these two syntaxes work?
Get-service spooler
Get-service 'spooler'

Answer : Because of the parser! The parser is responsible for presenting the information sent by the console or by the script in an executable way for Powershell. There are two modes, which explains why a string in an argument doesn’t require a quote (unless there’s a space) unlike a string in a variable: - Argument mode - Expression mode

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing?view=powershell-5.1

Video : Powershell - single quote, double quotes and protection

Related links