{"id":92902,"date":"2021-11-24T15:56:03","date_gmt":"2021-11-24T15:56:03","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=92902"},"modified":"2021-11-24T15:56:03","modified_gmt":"2021-11-24T15:56:03","slug":"working-with-powershell-strings","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/sysadmin\/powershell\/working-with-powershell-strings\/","title":{"rendered":"Working with PowerShell strings"},"content":{"rendered":"<p>One of the things that I\u2019ve both appreciated about PowerShell but also been at times confused by PowerShell is its concepts of strings and variables. This article explains what you need to know when working with PowerShell strings.<\/p>\n<p>For example, when you start writing in PowerShell, you may notice that you can use single quotes or double quotes to designate a string.<\/p>\n<h2>Starting simple<\/h2>\n<pre class=\"lang:ps theme:powershell-ise\">\"A very simple string.\"\r\n$string1 = \"This is a string variable.\"\r\n$string2 = 'This is also a string variable.'\r\n$string1, $string2\r\nwrite-host $string1, $string2\r\nwrite-host $string1 $string2<\/pre>\n<p>Save the above as <em>SimpleStrings_demo.ps1<\/em>. And as usual, examples are available at <a href=\"https:\/\/github.com\/stridergdm\/SimpleTalk_PowerShell-Scripts\/tree\/master\/PowerShell%20Strings\">Github<\/a>.<\/p>\n<p>When you run the above, you will see the following output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92903\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/word-image-16.png\" alt=\"Image showing output of script. Sometimes, there is a new line\" width=\"1237\" height=\"128\" \/><\/p>\n<p>The first line is echoed as is because you haven\u2019t told PowerShell to do anything special.<\/p>\n<p>Then you have created two variables and assigned strings to them. Again, in the case where there\u2019s no assignment and no cmdlet used, PowerShell simply echos out the contents of the variables, one to a line.<\/p>\n<p>When you use the write-host variable, PowerShell will again write them out, but this time keep them on the same line. It doesn\u2019t matter here if you use a comma or not to separate them.<\/p>\n<p>So far, the strings with double quotes and single quotes act the same way. But there is a difference which you\u2019ll see in the following example:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">$name = \"Greg\"\r\n$age = 53\r\n\"My name is $name and my age is $age.\"\r\n$string1 = \"DQ: My name is $name and my age is $age.\"\r\n$string2 = 'SQ: My name is $name and my age is $age.'\r\nwrite-host $string1\r\nwrite-host $string2\r\n <\/pre>\n<p>Save the above as <em>SimpleStrings_with_variables_demo.ps1<\/em> and run it.<\/p>\n<p>Your output will look like:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92904\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/word-image-17.png\" alt=\"Image showing that variable not replaced in single-quote string\" width=\"1219\" height=\"83\" \/><\/p>\n<p>You will immediately notice that in the case of the double quotes, PowerShell did replace the inline variables of <code>$name<\/code> and <code>$age<\/code> with their values. However, in the case of single quotes, it treated the variables as part of the string itself and did not replace them.<\/p>\n<p>The upside is that it\u2019s very easy to write out strings with the value of the variables embedded directly in the string. The downside is that you can\u2019t easily print out the variable&#8217;s name, which you may want to do while debugging a script.<\/p>\n<p>The following example, <em>SimpleStrings_with_variables_demo_2.ps1<\/em> shows a few ways of solving that problem:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">$name = \"Greg\"\r\n$age = 53\r\n\"My name is $name and my age is $age.\"\r\n$string1 = \"The `$name value is $name and the `$age value is $age.\"\r\n$string2 = 'The $name value is ' + $name + ' and the $age value is ' + $age\r\n$string3 = 'The $name value is ' + $name + \" and the `$age value is $age\"\r\nwrite-host $string1\r\nwrite-host $string2\r\nwrite-host $string3<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92905\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/word-image-18.png\" alt=\"Image showing other ways to add variable to string\" width=\"1220\" height=\"97\" \/><\/p>\n<p>You will notice the backtick character ` allows you to escape the $ so that PowerShell does not treat the characters that follow as a variable name. That is generally the way I do it.<\/p>\n<p>You may be tempted, and it\u2019s perfectly legal, to build a string as shown in <code>$string2<\/code>, however, I find that harder to read and more complex and see no value to it. <code>$string3<\/code> is simply an example showing you can use both single-quoted strings and double-quoted strings when building up a larger string. However, I would definitely avoid that one.<\/p>\n<h2>Dealing with objects<\/h2>\n<p>You will often find yourself dealing with objects when working with PowerShell. Dealing with objects introduces an issue, but fortunately, like most things, PowerShell has a solution. The following examples can be found in the Github repository in the file <code>Strings_with_object.ps1.<\/code><\/p>\n<p>Start by creating an object. For the purposes of this demo, it will be a simple instantiation and assignment.<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">$person1= @{Name='Greg' \r\nAge=53} <\/pre>\n<p>This object has two properties: a name and an age. Again if you simply hand the object to PowerShell and execute it, PowerShell will print something out, but a bit differently from the above examples.<\/p>\n<p>Pass <code>$person1<\/code> to PowerShell and hit enter, and you should see something like:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92906\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/graphical-user-interface-application-description-2.png\" alt=\"Image showing tabular results\" width=\"742\" height=\"121\" \/><\/p>\n<p>This method is not terrible, but also not terribly useful. However, if you try to use the write-host cmdlet as follows: <code>write-host $person1<\/code>, you don\u2019t get what you might expect. Instead, you get the curious response of:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92907\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/word-image-19.png\" alt=\"Image showing that printing object shows type not value\" width=\"810\" height=\"46\" \/><\/p>\n<p>That\u2019s not overly helpful. If you try to put the object in between double quotes (since you know with single quotes, it will simply print out what it is handed), you will get the following:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">write-host \"This is all about $person1\" <\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92908\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/word-image-20.png\" alt=\"Image showing that adding $ to object name doesn't print value inside a string\" width=\"688\" height=\"31\" \/><\/p>\n<p>Note the difference in the two responses. In the first case, PowerShell is attempting to parse the object and determine two properties within the object. In the second case, PowerShell doesn\u2019t do any parsing but simply determines it is a hashtable.<\/p>\n<p>The above responses provide the clues you need to get what you want. But before you get there, try the following:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">$person1.Name, $person1.Age <\/pre>\n<p>You will get:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92909\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/word-image-21.png\" alt=\"Image showing you can print object property\" width=\"883\" height=\"43\" \/><\/p>\n<p>This result makes sense because now you are specifying which property you want, but it\u2019s still far from perfect.<\/p>\n<p>You may try the following, which seems like it should work:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">write-host \"This is all about $person1.Name\" <\/pre>\n<p>But, curiously, this fails with the following:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92919 size-full\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/hashtablename.png\" alt=\"Image showing that object variable with property is not replaced in string\" width=\"888\" height=\"28\" \/><\/p>\n<p>Note that this message is a bit different from the above one that mentioned this being a Hashtable. It has <code>.Name<\/code> appended to the end. It appears that PowerShell is correctly trying to expand the object, but it\u2019s ignoring your reference to a specific property.<\/p>\n<p>The solution here is to force PowerShell to evaluate the entire object before it gets to <code>Write-Host<\/code>.<\/p>\n<p>You may be tempted to try the following:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\"># A work around\r\n$personName = $person1.Name\r\nwrite-host \"This is all about $personName\" <\/pre>\n<p>Executing this will work:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92920 size-full\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/about-greg.png\" alt=\"image showing the results of a work around\" width=\"564\" height=\"33\" \/><\/p>\n<p>But, it\u2019s wordy and, in my opinion, a bit messy. Imagine you had an object with a dozen or more properties you wanted to write out.<\/p>\n<p>The solution is to take advantage of scoping and to use parentheses as follows:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">write-host \"This is all about $($person1.Name) who is $($person1.Age)\" <\/pre>\n<p>This code will return:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92921 size-full\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/greg53.png\" alt=\"Image showing the result of using parentheses\" width=\"516\" height=\"34\" \/><\/p>\n<p>This result is most likely what you wanted. PowerShell starts with the object inside the parentheses (e.g. <code>$person1.Name<\/code>) and evaluates that, and then treats it as a variable denoted by the outer $. Write-host then evaluates that variable and inserts it into the string.<\/p>\n<p>Note this means you can do some additional fancy things such as the following<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">$person2 = @{Name=\"Joe\"\r\nAge=78}\r\nwrite-host \"Together $($person1.Name) and $($person2.Name) are a total of $($person1.Age + $person2.Age) years old.\" <\/pre>\n<p>This will produce:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92922 size-full\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/gregandjoe.png\" alt=\"Image showing summation of values from object\" width=\"807\" height=\"39\" \/><\/p>\n<p>The summation of the ages of <code>$person1<\/code> and <code>$person2<\/code> happens before <code>Write-host<\/code> evaluates the outer $.<\/p>\n<h2>Formatting strings<\/h2>\n<p>Imagine you are tasked with using PowerShell to print out invoices from an order management system. With your newfound knowledge, you sit down and write something like:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">$cost = 144.56\r\n$quantity = 11\r\nWrite-host \"Your cost for $quantity will be: $($cost*$quantity)\" <\/pre>\n<p>Your output looks like:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92923 size-full\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/costfor11.png\" alt=\"Image showing output, cost for 11\" width=\"568\" height=\"39\" \/><\/p>\n<p>It\u2019s not quite perfect. You\u2019d really like to include a currency character to the string, but you can come back to that later.<\/p>\n<p>The more immediate issue is that when <code>$quantity = 10<\/code>. Then you get the following:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-92924\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/costfor10.png\" alt=\"\" width=\"520\" height=\"28\" \/><\/p>\n<p>Note the missing trailing 0.<\/p>\n<p>This problem is fortunately easy to fix. PowerShell strings allow formatting using .NET standards.<\/p>\n<p>One simple way of fixing the above is the code below:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">$formatted = \"Your cost for {0} will be: {1:f2}\" -f $quantity, $($quantity*$cost)\r\nwrite-host $formatted <\/pre>\n<p>This will print:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-92925\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/costfor10with0.png\" alt=\"\" width=\"522\" height=\"37\" \/><\/p>\n<p>That\u2019s an improvement. The <code>f2<\/code> is an instruction to return 2 floating-point values after the period. Additionally, you may want to put a currency symbol in there and a comma.<\/p>\n<p>A simple fix would simply to do the following:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">$formatted = \"Your cost for {0} will be: `${1:n2}\" -f $quantity, $($quantity*$cost)\r\nwrite-host $formatted <\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92926 size-full\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/dollar.png\" alt=\"Image showing output with $\" width=\"612\" height=\"46\" \/><\/p>\n<p>Note two essential parts here. The dollar sign was added with a backtick ` so PowerShell doesn\u2019t interpret it as the start of a variable name. In addition, by changing the <code>f<\/code> to <code>n<\/code>, PowerShell will treat this as a number using a cultural separator. This code will work equally well in a machine set up in Europe, where they tend to reverse the use of the period and comma separators. However, the currency character is still an issue which can be fixed with slightly different formatting:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">$formatted = \"Your cost for {0} will be: {1:c2}\" -f $quantity, $($quantity*$cost)\r\nwrite-host $formatted <\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92926 size-full\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/dollar.png\" alt=\"Image showing output with currency symbol\" width=\"612\" height=\"46\" \/><\/p>\n<p>Note that the <code>$<\/code> is no longer needed as part of the string.<\/p>\n<p>PowerShell isn\u2019t just limited to formatting currency; you can use it for other formats. For example, say you needed to generate a list of User IDs with a specific format:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">1..15 | % { 'UserID_{0:d4}' -f $_ } <\/pre>\n<p>The above will generate 15 User IDs of the format:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92910\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/shape-description-automatically-generated.png\" alt=\"Image showing 15 user ids generated\" width=\"1140\" height=\"322\" \/><\/p>\n<p>A common requirement is formatting dates. There\u2019s a couple of ways of doing this. If you do nothing and simply accept the defaults as follows:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">$exampledate = get-date\r\nwrite-host $exampledate <\/pre>\n<p>The results depend on your regional settings. In the US, you will get:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92911\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/word-image-22.png\" alt=\"Image showing date and time\" width=\"578\" height=\"21\" \/><\/p>\n<p>You could format the date when you create it:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">$fdate = get-date -Format \"yyyy-MM-dd\"\r\nwrite-host $fdate <\/pre>\n<p>This will return:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92912\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/word-image-23.png\" alt=\"Image showing date only\" width=\"276\" height=\"17\" \/><\/p>\n<p>Unfortunately, the variable <code>$fdate<\/code> contains only the information shown. You can\u2019t later reference the time if you want.<\/p>\n<p>However, there are a lot of built-in formats available:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">$fdate = get-date -Format \"R\"\r\nwrite-host $fdate <\/pre>\n<p>This method returns:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92913\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/word-image-24.png\" alt=\"Image showing date with special formatting\" width=\"310\" height=\"19\" \/><\/p>\n<p>If you want to store the full value of the returned date and later use only a formatted portion of it, you can do something like the following:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">write-host $exampledate.ToString('yyyy-MM-dd')\r\nWrite-Host $exampledate.ToString(\"hh:mm:ss\")<\/pre>\n<p>As you can see, there are plenty of ways to format your strings, dates, and other variables.<\/p>\n<h2>String padding and trimming<\/h2>\n<p>Often you may find yourself wanting to pad out or trim a string. You may want items to appear clearly in columns on the screen, or you may write to a file with fixed-length fields.<\/p>\n<p>This task can be easily handled, as seen in the examples below. Save these as <em>String_Padding_and_Trimming.ps1.<\/em><\/p>\n<p>First, you need to generate some strings. Note that while the first variable claims to have three characters, it actually has a trailing space. This might go unnoticed in some cases if the value was read in from a database field.<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">$3char = \"123 \"  # note space!\r\n$4char = \"4567\"\r\nwrite-host $3char\r\nWrite-Host $4char<\/pre>\n<p>This looks perfectly fine as is:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92914\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/word-image-25.png\" alt=\"Image showing numbers don't line up on right\" width=\"239\" height=\"36\" \/><\/p>\n<pre class=\"lang:ps theme:powershell-ise\"># But what if we want them to line up, and we expect someday we might even have longer strings.\r\n$padamount = 5\r\n$padcharacter = ' '\r\nwrite-host $3char.PadLeft($padamount,$padcharacter)\r\nWrite-Host $4char.PadLeft($padamount,$padcharacter)<\/pre>\n<p>This now almost works, but that trailing space is obvious:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92915\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/word-image-26.png\" alt=\"Image showing numbers don't line up because of trailing space\" width=\"230\" height=\"34\" \/><\/p>\n<p>Fortunately, this is fairly easy to fix using the <code>trim()<\/code> method:<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">write-host $3char.trim().PadLeft($padamount,$padcharacter)\r\nWrite-Host $4char.PadLeft($padamount,$padcharacter) <\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92916\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/word-image-27.png\" alt=\"Image showing that using trim will align numbers on right\" width=\"230\" height=\"34\" \/><\/p>\n<p>You can also pad to the right and, of course, select other characters. Again notice the trailing space is an issue, so in the second example, you can eliminate it.<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">$padamount = 7\r\n$padcharacter = '.'\r\nwrite-host $3char.PadRight($padamount,$padcharacter)\r\nWrite-Host $4char.PadRight($padamount,$padcharacter)<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92917\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/word-image-28.png\" alt=\"Image showing dots with numbers to show where the trailing space is\" width=\"246\" height=\"35\" \/><\/p>\n<pre class=\"lang:ps theme:powershell-ise\">write-host $3char.trim().PadRight($padamount,$padcharacter)\r\nWrite-Host $4char.PadRight($padamount,$padcharacter) <\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"247\" height=\"34\" class=\"wp-image-92918\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/11\/word-image-29.png\" \/><\/p>\n<p>You can do a lot more with the Pad and Trim methods once you fully understand them.<\/p>\n<h2>Adding some color to your life<\/h2>\n<p>Finally, what is life without some color, or, in this case, showing strings with color?<\/p>\n<p>Like most things, PowerShell makes this very easy. Below is a script that was possibly written by a Hobbit to keep track of something near and dear to their stomach.<\/p>\n<p>Save the following script <em>as String_Color.ps1.<\/em><\/p>\n<pre class=\"lang:ps theme:powershell-ise\">$tod = get-date\r\nif ($tod.Hour -ge 6 -and $tod.Hour -lt 9)\r\n    {\r\n        write-host \"Time for Breakfast!\" -ForegroundColor Yellow\r\n    }\r\nelseif ($tod.Hour -ge 9 -and $tod.Hour -lt 11)\r\n    {\r\n        write-host \"Time for 2nd Breakfast!\" -ForegroundColor Red -BackgroundColor Green\r\n        write-host \"That's only if he knows about second breakfast!\"           \r\n    }    \r\nelseif ($tod.Hour -ge 11 -and $tod.Hour -lt 12)\r\n    {\r\n        write-host \"Elevenses!\" -BackgroundColor Red\r\n    }    \r\nelseif ($tod.Hour -ge 12 -and $tod.Hour -lt 14)\r\n    {\r\n        write-host \"Luncheon\" -ForegroundColor Red -BackgroundColor Yellow\r\n    }\r\nelse\r\n    {\r\n        Write-host \"Afternoon Tea - 2:00 PM until 5:00 PM \" -ForegroundColor Black -BackgroundColor DarkRed -NoNewline\r\n        Write-host \"Dinner - 7:00 PM until 9:00 PM \" -ForegroundColor Red -BackgroundColor DarkBlue -NoNewline\r\n        Write-host \"Supper - 9:00 PM until 11:00 PM \" -ForegroundColor Red -BackgroundColor DarkGreen\r\n    } \r\n     <\/pre>\n<p>The above script will help any Hobbit keep track of what meal they should be planning for. Note that portion control in case 12 dwarves show up unexpectedly is not covered.<\/p>\n<p>If it\u2019s early in the day, the script will simply change the foreground color of the text to yellow. This color change is done merely by adding the \u2013foreground parameter to write-host.<\/p>\n<p>In the case of second breakfast, you can set both the foreground and background colors as you desire. You will also note that any color you set is only in effect for that particular write-host cmdlet. The following one will revert back to the host colors in your terminal or IDE.<\/p>\n<p>You can, as demonstrated with Elevenses, set only the background color.<\/p>\n<p>Finally, if you want to have multiple colors on the same line, you can take advantage of the \u2013NoNewLine parameter for Write-Host and suppress adding a newline character to your output. This technique can be useful if you want to build a menu option such as below: (save as <em>Select_Option_With_Color.ps1<\/em>)<\/p>\n<pre class=\"lang:ps theme:powershell-ise\">write-host \"1) \" -ForegroundColor Red -NoNewline\r\nwrite-host \"select Option 1\" -ForegroundColor Yellow\r\nwrite-host \"2) \" -ForegroundColor Red -NoNewline\r\nwrite-host \"select Option 2\" -ForegroundColor Yellow\r\nwrite-host \"3) \" -ForegroundColor Red -NoNewline\r\nwrite-host \"select Option 3\" -ForegroundColor Yellow\r\n$result = Read-host \"Select an option above\"\r\nif ($result -ge 1 -and $result -le 3)\r\n    { Write-host \"Thank you for selecting $result\" }\r\nelse\r\n    { \"You selected an invalid value $result\" }\r\n <\/pre>\n<h2>PowerShell strings<\/h2>\n<p>In general, string handling in PowerShell is quite simple, but minor differences such as using double quotes or single quotes can make a difference. In addition, knowing how to escape a character or force the evaluation of a variable inside a string can make life simpler as you don\u2019t need to add additional steps before you can output your string.<\/p>\n<p>Additionally, most of the string operations shown in the article can be used when outputting to a file, making the creation of a CSV or file based on length delimited columns far easier to create.<\/p>\n<p><em>If you like this article, you might also like\u00a0<a href=\"https:\/\/www.red-gate.com\/simple-talk\/sysadmin\/powershell\/powershell-editors-and-environments-part-2\/\">PowerShell editors and environments part 2.<\/a><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Knowing how to manipulate strings is critical in any language. In this article, Greg Moore explains working with PowerShell strings.&hellip;<\/p>\n","protected":false},"author":319367,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[53,35],"tags":[95506],"coauthors":[61343],"class_list":["post-92902","post","type-post","status-publish","format-standard","hentry","category-featured","category-powershell","tag-automate"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/92902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/319367"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=92902"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/92902\/revisions"}],"predecessor-version":[{"id":92928,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/92902\/revisions\/92928"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=92902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=92902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=92902"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=92902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}