{"id":16681,"date":"2010-10-28T00:01:00","date_gmt":"2010-10-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/10\/28\/use-microsoft-net-framework-string-methods-from-windows-powershell\/"},"modified":"2010-10-28T00:01:00","modified_gmt":"2010-10-28T00:01:00","slug":"use-microsoft-net-framework-string-methods-from-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-microsoft-net-framework-string-methods-from-windows-powershell\/","title":{"rendered":"Use Microsoft .NET Framework String Methods from Windows PowerShell"},"content":{"rendered":"<p><span style=\"font-family: courier new,courier\"><\/span>&nbsp;<\/p>\n<p><b>Summary:<\/b> Microsoft Scripting Guy Ed Wilson teaches how to use .NET Framework string methods from&nbsp;within Windows PowerShell,<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/>Hey, Scripting Guy! There are many things I need to do with strings and with text files that do not seem to be supported with Windows PowerShell. It is strange, but many common operations seem to have been neglected. I saw somewhere a technique for running VBScript code from within Windows PowerShell, and I am thinking about trying that out. What do you think is it doable?<\/p>\n<p>&#8212; PW<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/>Hello PW, Microsoft Scripting Guy Ed Wilson here. Working with the .NET Framework is always fun. It almost seems like a trick to be able to use such a powerful resource from a mere scripting language. <\/p>\n<p>Because Windows PowerShell resides upon the .NET Framework, it means I have access to .NET Framework classes from inside Windows PowerShell. One of the most important classes for Windows PowerShell scripters is the <b>System.String<\/b> class. I looked at the <b>System.String<\/b> class <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/10\/27\/use-net-framework-classes-to-work-with-strings-in-windows-powershell.aspx\">yesterday<\/a>. <\/p>\n<p>I want to continue exploring the <b>System.String<\/b> class today by looking at the <b>EndsWith<\/b> method. The <b>EndsWith<\/b> method returns a Boolean value if a string ends with a particular string value. In the example that follows a string is stored in the <i>$a<\/i> variable. To examine several string ending scenarios use the <b>EndsWith<\/b> method. A single letter match returns true, and a single letter that does not match returns false. If a partial word letter sequence matches a sequence of letters in the last word, then it returns true. Wildcard characters match as literals and therefore return unexpected results. The code that follows shows these scenarios. <\/p>\n<p><span style=\"font-family: courier new,courier\">PS C:\\&gt; $a = &#8220;this is a string&#8221;<br \/>PS C:\\&gt; $a.EndsWith(&#8220;g&#8221;)<br \/>True<br \/>PS C:\\&gt; $a.EndsWith(&#8220;e&#8221;)<br \/>False<br \/>PS C:\\&gt; $a.EndsWith(&#8220;ring&#8221;)<br \/>True<br \/>PS C:\\&gt; $a.EndsWith(&#8220;sring&#8221;)<br \/>False<br \/>PS C:\\&gt; $a.EndsWith(&#8220;string&#8221;)<br \/>True<br \/>PS C:\\&gt; $a.EndsWith(&#8220;str*&#8221;)<br \/>False<br \/>PS C:\\&gt; $a.EndsWith(&#8220;strin*&#8221;)<br \/>False<br \/>PS C:\\&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>An interesting method is the <b>clone<\/b> method. It is interesting not from the perspective of direct utility but from a readability point of view. The <b>clone<\/b> method copies the object that is contained in a variable. In the example seen here, I use the <b>clone<\/b> method to <b>clone<\/b> the string stored in the <i>$a<\/i> variable and to store the resultant copy in the <i>$b<\/i> variable. I then use direct assignment to store the contents of <i>$a<\/i> in the <i>$c<\/i> variable. The effect, in this scenario is the same, but there are differences in the actual behavior of the two operations. The code is shown here. <\/p>\n<p><span style=\"font-family: courier new,courier\">PS C:\\&gt; $a = &#8220;a new string&#8221;<br \/>PS C:\\&gt; $b = $a.Clone()<br \/>PS C:\\&gt; $b<br \/>a new string<br \/>PS C:\\&gt; $c = $a<br \/>PS C:\\&gt; $c<br \/>a new string<br \/>PS C:\\&gt; $b.GetType()<\/p>\n<p>IsPublic IsSerial Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BaseType<br \/>&#8212;&#8212;&#8211; &#8212;&#8212;&#8211; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8211;<br \/>True&nbsp;&nbsp;&nbsp;&nbsp; True&nbsp;&nbsp;&nbsp;&nbsp; String&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Object<\/p>\n<p>PS C:\\&gt; $c.GetType()<\/p>\n<p>IsPublic IsSerial Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BaseType<br \/>&#8212;&#8212;&#8211; &#8212;&#8212;&#8211; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8211;<br \/>True&nbsp;&nbsp;&nbsp;&nbsp; True&nbsp;&nbsp;&nbsp;&nbsp; String&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Object<\/p>\n<p>PS C:\\&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Occasionally I have to insert text into the one that is stored in a variable. To do this requires two steps. The first step involves determining the insertion point, and the second step is the actual insertion itself. Several methods can determine the insertion point for a string. There is only one <b>insert<\/b> method. If I need to add a string to the end of a string, I can use the <b>length<\/b> property of the string to determine my insertion point. The code that appears here shows this technique. <\/p>\n<p><span style=\"font-family: courier new,courier\">$a = &#8220;This is a string&#8221;<\/span><\/p>\n<p><span style=\"font-family: courier new,courier\">PS C:\\&gt; $a.Insert($a.length,&#8221; that is cool&#8221;)<\/span><\/p>\n<p><span style=\"font-family: courier new,courier\">This is a string that is cool<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Instead of going to all the trouble of using the <b>insert<\/b> method to add to the end of a string stored in a variable, I would probably use concatenation as seen here. <\/p>\n<p><span style=\"font-family: courier new,courier\">PS C:\\&gt; $a = &#8220;This is a string&#8221;<\/span><\/p>\n<p><span style=\"font-family: courier new,courier\">PS C:\\&gt; $a + &#8221; that is cool&#8221;<\/span><\/p>\n<p><span style=\"font-family: courier new,courier\">This is a string that is cool<\/span><\/p>\n<p><span style=\"font-family: courier new,courier\">PS C:\\&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Concatenation cannot perform sophisticated types of string augmentation. The <b>LastIndexOf<\/b> method locates a character or series of characters inside a string. This location becomes the insertion point. In the code that appears here the <b>LastIndexOf<\/b> method locates the letter a in the string stored in the <i>$a<\/i> variable. The first try does not work due to failure to account for the need for a space before the insertion string. In addition, it fails because the <b>LastIndexOf<\/b> method returns a zero-based number. The last command corrects both problems. <\/p>\n<p><span style=\"font-family: courier new,courier\">PS C:\\&gt; $a = &#8220;This is a string&#8221;<\/span><\/p>\n<p><span style=\"font-family: courier new,courier\">PS C:\\&gt; $a.insert($a.LastIndexOf(&#8220;a&#8221;),&#8221;really big&#8221;)<\/span><\/p>\n<p><span style=\"font-family: courier new,courier\">This is really biga string<\/span><\/p>\n<p><span style=\"font-family: courier new,courier\">PS C:\\&gt; $a.insert($a.LastIndexOf(&#8220;a&#8221;) +1,&#8221; really big&#8221;)<\/span><\/p>\n<p><span style=\"font-family: courier new,courier\">This is a really big string<\/span><\/p>\n<p><span style=\"font-family: courier new,courier\">PS C:\\&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Sometimes I have to replace a word in a string that is stored in a variable. The <b>Replace<\/b> method makes quick work of these cases. The syntax is <b>replace(original, replacement)<\/b>. In the code that follows a string is stored in the <i>$a<\/i> variable. The <b>Replace<\/b> method replaces the word <b>string<\/b> with the word <b>phrase<\/b>. The output displays to the Windows PowerShell console but is not stored in either the original variable or a new variable. This code appears here. <\/p>\n<p>PS C:\\&gt; $a = &#8220;This is a string&#8221;<\/p>\n<p>PS C:\\&gt; $a.Replace(&#8220;string&#8221;,&#8221;phrase&#8221;)<\/p>\n<p>This is a phrase<\/p>\n<p>PS C:\\&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>In addition to using the <b>Replace<\/b> method from the <b>System.String<\/b> class, Windows PowerShell provides a replace operator that works in a similar manner as the Replace method. Most of the time I will use the <b>Replace<\/b> method from the <b>String <\/b>class because tab expansion will complete the command for me. In the code seen here, tab expansion does not work for the <b>replace <\/b>operator. Therefore, the command requires manual typing into the Windows PowerShell console. As seen here, the command and the output matches well to the <b>Replace <\/b>method from the<b> String<\/b> class.<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">PS C:\\&gt; $a = &#8220;This is a string&#8221;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">PS C:\\&gt; $a -replace(&#8220;string&#8221;,&#8221;phrase&#8221;)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">This is a phrase<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">PS C:\\&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>A common scenario is parsing text from an array. Whether the array comes from reading a text file through the <b>Get-Content<\/b> cmdlet, or is a hard-coded array such as the one shown here, the <b>String<\/b> methods work in the same way as they did with a single string. In the code example seen here, an array of computer names is created and stored in the <i>$a<\/i> variable. When the contents of the <i>$a<\/i> variable displays each computer name is listed on a single line. The <b>IndexOf<\/b> method locates the colon that follows the word computer in each element of the array. It appears in position 8. When the <b>SubString<\/b> method is used to retrieve all the text following starting position 9 the space was included in the text that returns. Adding an additional space to allow for the starting position corrects the output. This code appears here. <\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">PS C:\\&gt; [array]$a = &#8220;computer: berlin&#8221;,&#8221;computer: hamburg&#8221;,&#8221;computer: munich&#8221;<br \/>PS C:\\&gt; $a<br \/>computer: berlin<br \/>computer: hamburg<br \/>computer: munich<br \/>PS C:\\&gt; $a | foreach-object { $_.indexof(&#8220;:&#8221;) }<br \/>8<br \/>8<br \/>8<br \/>PS C:\\&gt; $a | foreach-object { $_.substring(9) }<br \/>&nbsp;berlin<br \/>&nbsp;hamburg<br \/>&nbsp;munich<br \/>PS C:\\&gt; $a | foreach-object { $_.substring(10) }<br \/>berlin<br \/>hamburg<br \/>munich<br \/>PS C:\\&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>The commands and their associated output appear in the following figure.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/3808.HSG-10-28-10-01.JPG\" border=\"0\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>PW, that is all there is to using .NET Framework <b>String<\/b> methods to manipulate strings with Windows PowerShell. This also ends .NET Framework week. Join me tomorrow when I open the virtual mail bag and answer a whole raft of puzzling questions-that&#8217;s right, it is time for Quick Hits Friday. <\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a target=\"_blank\" href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> or post them on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Summary: Microsoft Scripting Guy Ed Wilson teaches how to use .NET Framework string methods from&nbsp;within Windows PowerShell, &nbsp; Hey, Scripting Guy! There are many things I need to do with strings and with text files that do not seem to be supported with Windows PowerShell. It is strange, but many common operations seem to [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,21,45],"class_list":["post-16681","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-string-manipulation","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Summary: Microsoft Scripting Guy Ed Wilson teaches how to use .NET Framework string methods from&nbsp;within Windows PowerShell, &nbsp; Hey, Scripting Guy! There are many things I need to do with strings and with text files that do not seem to be supported with Windows PowerShell. It is strange, but many common operations seem to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=16681"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16681\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=16681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}