Alisdair Craik

Alisdair Craik

N/A

PowerShell Tutorial – Building Strings and Inserting Variables With ‘{0}’

We’ve seen a few questions lately about building up strings in Powershell and inserting variables into the text, so we thought we would base this tutorial on the easiest and simplest way to construct a string when you want to be absolutely certain of what you are going to get. So, this post is all about using placeholders in strings and substitution…

Getting an object to work with:

Before we start, we’re going to need an object to work with in our examples, so that we can insert its properties into a string. We’re going to use the Vexasoft cmdlet Get-NetworkAdapter to collect IP and hardware information. By running the command $NIC = Get-NetworkAdapter -Computer PC01 the $NIC variable is populated with the following object:

Name: Local Area Connection
Description: Intel(R) PCI Fast Ethernet Controller
IPAddress: 192.168.0.44
Subnet: 255.255.255.0
Gateway: 192.168.0.1
PrimaryDNS: 8.8.8.8
MACAddress: 00:02:EE:44:96:D5
Slot: PCI 12
Speed: 1000 Mbps
DHCPEnabled: False
RegisterDNS: True
HostName: PC01
Status: Connected

We can now build our string using the properties of $NIC.

Using Format Placeholders:

Powershell is built on .Net and contains all the goodness therein. By using the PowerShell implementation of .Net’s String.Format method it is very easy to insert variables (or other objects) into strings in PowerShell. String.Format is based on using placeholders to identify the areas to substiture in a string, and the PowerShell equivalent is no different. Placeholders are written directly into the string where you want your object or variable to appear. They are written in the form curly brace, number, curly brace and count upwards from zero – so a string with three placeholders will look something like this:

‘{0} has the IPAddress {1} and the MACAddress {2}’

To use placeholders, you then need to specify the objects you wish to substitute for the each item – you do this by using the parameter -f (for format) directly after the string and supplying a comma separated list of objects to substitute for the placeholders, in the order you wish them to be substituted:

‘{0} has the IPAddress {1} and the MACAddress {2}’ -f $NIC.HostName, $NIC.IPAddress, $NIC.MACAddress

Returns: PC01 has the IPAddress 192.168.0.44 and the MACAddress 00:02:EE:44:96:D5

You will notice that by using placeholders we are able to use single quotes around the string (‘), meaning we do not have to worry about how the rest of the string will appear – it will show exactly as typed. We can also perform actions on the variables or use variable properties without having to worry that the working out will be inserted into the string instead of the result. For example:

‘$100 plus seven percent sales tax is ${0}’ -f (100*1.07)

Returns: $100 plus seven percent sales tax is $107

You can also perform whole cmdlets when substituting placeholders:

$CN=’PC01′
‘Computer {0} has serial number {1}.’ -f $CN, (Get-Serial $CN)

Returns: Computer PC01 has serial number JHF865TG.

You can also use the same placeholder more that once in a string:

‘c:\users\{0}\contacts\{0}.contact’ -f ‘asmith’

Returns: c:\users\asmith\contacts\asmith.contact

Lastly, it should be noted that you must specify an object for each placeholder, or Powershell will throw an error:

‘c:\{0}\{1}’ -f $directory

Returns: An error, because there is no variable supplied for {1}.

And that’s about it. There are numerous other ways to build up strings in Powershell but this is by far our preferred method here and should keep you going for the vast majority of scenarios. If you are interested in more advanced string operations using the .Net Framework in Powershell there’s an excellent post over on The Scripting Guy blog about working with strings in Powershell using .Net.

<< Back To The Blog