Powershell Strings and Expressions


Today's topic? PowerShell, Strings, Variable Expansion and Quotes!

Let's start with the difference between the single and double quotes. When using single quotes, the dollar sign within a string doesn't get treated as a special character so the text following it does not get resolved to the corresponding variable as opposed to the double quotes. The "file" variable in the following examples holds the output object of the Get-Item cmdlet.


Variable expansion within strings... Let me start with an example. We have an object that has multiple properties and we want to create a string using the value of one of those properties. We have to use double quotes since we want the variable to be resolved but the result isn't the desired one.

 
This is because PowerShell will replace the variable with it's string representation and the property will be interpreted as text.


This is where the PowerShell expressions come into play. Enclosing the variable and the property in parentheses after a dollar sign will first evaluate the expression and then insert it into the string.


Popular posts from this blog

Domain Controller Machine Password Reset

Configuring a Certificate on Exchange Receive Connector

Running Multiple NGINX Ingress Controllers in AKS