{"id":3261,"date":"2013-07-10T00:01:00","date_gmt":"2013-07-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/07\/10\/a-powershell-function-to-count-string-length\/"},"modified":"2013-07-10T00:01:00","modified_gmt":"2013-07-10T00:01:00","slug":"a-powershell-function-to-count-string-length","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/a-powershell-function-to-count-string-length\/","title":{"rendered":"A PowerShell Function to Count String Length"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, talks about creating a Windows PowerShell function that counts a string length and copies it to the clipboard.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. It is hot and humid outside&mdash;89 degrees Fahrenheit (32 degrees Celsius) and 75% humidity. That was the great thing about Madrid. Whereas, it was a little hotter (92 degrees Fahrenheit, 33 degrees Celsius), the humidity was less than half the amount we have in Charlotte, North Carolina. The Scripting Wife and I are soaked following a quick trip to the library&mdash;and we were not even running. I am sipping a cup of Oolong Green Tea with a bit of cinnamon stick and rose hips. Believe it or not, hot tea on a hot summer day is great. It makes me feel cooler.<\/p>\n<p>My personal criteria for writing a Windows PowerShell script or a Windows PowerShell function is: when a task becomes annoying to me, I spend the time to create a function or a script. At times, the scenario is not all that bad, but eventually it becomes annoying. Today&rsquo;s function provides such an example. I need to determine the character length of the title for my Hey, Scripting Guy! Blog. To do this, I have been opening Notepad and using the word count feature there. Then I copy the text to the clipboard, close Notepad, and paste it into Word 2013. So here are the steps involved:<\/p>\n<ol>\n<li>Open Notepad.<\/li>\n<li>Type the title.<\/li>\n<li>Note the number of characters.<\/li>\n<li>Copy the title to the clipboard.<\/li>\n<li><span style=\"font-size: 12px\">Select the entire text.<\/span><\/li>\n<li><span style=\"font-size: 12px\">Type Ctrl-C.<\/span><\/li>\n<li><span style=\"font-size: 12px\">Close Notepad.<\/span><\/li>\n<li><span style=\"font-size: 12px\">Paste the title into Word 2013.<\/span><\/li>\n<\/ol>\n<p>When I list out the number of steps, it seem like a lot of work, but in reality it takes only a minute or two to accomplish. Hmmm&hellip;w<span style=\"font-size: 12px\">ith 730 Hey, Scripting Guy! Blog posts a year, at two minutes each&hellip;<\/span><\/p>\n<p>That equals 24.5 hours of work! &nbsp;My new function allows me to accomplish this in less than 15 seconds. Therefore, I save 18.3 hours a year. That is not a bad ROI for a function that took me less than 20 minutes to write.<\/p>\n<h2>Creating a function to count letters and copy to the clipboard<\/h2>\n<p>So I decided to create a function to count the characters in a string. I am going to add the function to my personal profile so it will always be available.<\/p>\n<h3>Start with a function outline<\/h3>\n<p>The first thing I do is begin with a function outline as shown here:<\/p>\n<p style=\"padding-left: 30px\">Function<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>Now I need to decide on the name. I always want to use an approved verb. This is easy, because there is the <strong>Get-Verb<\/strong> command. At first, I was thinking <em>Count. <\/em>But when I used the command shown here, there is no <em>Count <\/em>as an approved verb.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; get-verb count<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt;&nbsp;<\/p>\n<p><span style=\"font-size: 12px\">So this means I will use the verb <\/span><em style=\"font-size: 12px\">Get <\/em><span style=\"font-size: 12px\">and then add something descriptive. I came up with <\/span><strong style=\"font-size: 12px\">Get-LetterCount<\/strong><span style=\"font-size: 12px\">. My function outline now looks like this:<\/span><\/p>\n<p style=\"padding-left: 30px\">Function Get-LetterCount<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">} # End Function Get-LetterCount<\/p>\n<h3>Add the function parameter<\/h3>\n<p>Now I need to add a parameter that I will use for input. I cast the input as a string because that is the way I anticipate using the function. Here is the <strong>Param<\/strong><em> <\/em>statement:<\/p>\n<p style=\"padding-left: 30px\">Param ([string]$string)<\/p>\n<p>The cool thing is that a string automatically has a <strong>Length<\/strong><em> <\/em>property so getting the information will be really easy. I want to display the output to the console. I use the <strong>Write-Host<\/strong> cmdlet to do this, as shown in this line of code:<\/p>\n<p style=\"padding-left: 30px\">Write-Host -ForegroundColor Cyan &#8220;string length is&#8221; $string.Length<\/p>\n<p>Now, I need to copy the string input to the clipboard. To do this, I use the <strong>Clip.exe<\/strong><em> <\/em>command. This is easy. Here is the code:<\/p>\n<p style=\"padding-left: 30px\">$string | clip.exe<\/p>\n<p>The completed function is shown here:<\/p>\n<p style=\"padding-left: 30px\">Function Get-LetterCount<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Param ([string]$string)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Write-Host -ForegroundColor Cyan &#8220;string length is&#8221; $string.Length<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$string | clip.exe<\/p>\n<p style=\"padding-left: 30px\">} # End Function Get-LetterCount<\/p>\n<h3>Clean up and make things more usable<\/h3>\n<p>I do not want to have to type <strong>Get-LetterCount<\/strong> each time I want to use the function. Therefore, I create a really short alias&mdash;I mean <em>really<\/em> short. The letter C. I use <strong>Set-Alias<\/strong> instead of <strong>New-Alias<\/strong> so that it will overwrite. This is important during testing. It is also something I need to do to ensure that I do not overwrite another alias. Therefore, I use the <strong>Get-Alias<\/strong> command first. This is shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Alias c<\/p>\n<p style=\"padding-left: 30px\">Get-Alias : This command cannot find a matching alias because an alias with the<\/p>\n<p style=\"padding-left: 30px\">name &#8216;c&#8217; does not exist.<\/p>\n<p style=\"padding-left: 30px\">At line:1 char:1<\/p>\n<p style=\"padding-left: 30px\">+ Get-Alias c<\/p>\n<p style=\"padding-left: 30px\">+ ~~~~~~~~~~~<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ObjectNotFound: (c:String) [Get-Alias], ItemNotFoundE<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; xception<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.G<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; etAliasCommand<\/p>\n<p>Cool. Now I can add my new alias as shown here:<\/p>\n<p style=\"padding-left: 30px\">Set-Alias -Name c -Value Get-LetterCount<\/p>\n<p>Now I add comment-based Help to the function. This takes me less than 60 seconds because I use my <strong>Add-Help<\/strong> function from my Windows PowerShell profile module.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> For more information about my Windows PowerShell profile module, see <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/05\/19\/weekend-scripter-add-power-and-functionality-to-the-powershell-ise-part-2.aspx\" target=\"_blank\">Add Power and Functionality to the PowerShell ISE Part 2<\/a><em>.<\/em><\/p>\n<p>Here is the comment-based Help:<\/p>\n<p style=\"padding-left: 30px\">&lt;#<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Synopsis<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; This counts letters in a string<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Description<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; This function counts letters in a string<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Example<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Get-LetterCount &#8220;this is a string<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Returns&nbsp; string length is 16 and copies text to clipboard<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Parameter String<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; The input as a string<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Notes<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; NAME:&nbsp; Get-LetterCount<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; AUTHOR: ed wilson, msft<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; LASTEDIT: 07\/03\/2013 12:33:28<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; KEYWORDS: Scripting Techniques, String Manipulation, Functions<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; HSG: HSG-7-10-13<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Link<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; Http:\/\/www.ScriptingGuys.com<\/p>\n<p style=\"padding-left: 30px\">&nbsp;#Requires -Version 2.0<\/p>\n<p style=\"padding-left: 30px\">&nbsp;#&gt;<\/p>\n<p><strong>Note<\/strong> &nbsp;For more information about adding comment-based Help, see <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/09\/11\/automatically-add-comment-based-help-to-your-powershell-scripts.aspx\" target=\"_blank\">Automatically Add Comment-Based Help to your PowerShell Scripts<\/a>.<em><\/em><\/p>\n<h2>Using the Get-LetterCount function<\/h2>\n<p>I add the <strong>Get-LetterCount<\/strong> function and the alias to my Windows PowerShell ISE profile. I can run the function from inside the ISE or dot source it in the Windows PowerShell console. After the function loads and the alias is created, I use the alias and pass the string as shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; c &#8220;A PowerShell Function to count string length&#8221;<\/p>\n<p style=\"padding-left: 30px\">string length is 44<\/p>\n<p>When I call the function, I use the <strong>Get-Clipboard<\/strong> cmdlet (from the PSCX module) to verify that my string is indeed on the clipboard. This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5722.hsg-7-10-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5722.hsg-7-10-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>I can now paste my tile into my Word document. Cool! Now, I need to decide what I will do with the two days I have saved with this function.<\/p>\n<p>The complete script is uploaded to the Script Center Repository: <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Get-String-Letter-Count-b3d77462\" target=\"_blank\">Get String Letter Count Function<\/a>.<\/p>\n<p>That is all there is to creating a Windows PowerShell function to count string length and copy the string to the clipboard. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about creating a Windows PowerShell function that counts a string length and copies it to the clipboard. Microsoft Scripting Guy, Ed Wilson, is here. It is hot and humid outside&mdash;89 degrees Fahrenheit (32 degrees Celsius) and 75% humidity. That was the great thing about Madrid. Whereas, it was [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[69,3,4,21,45],"class_list":["post-3261","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-functions","tag-scripting-guy","tag-scripting-techniques","tag-string-manipulation","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about creating a Windows PowerShell function that counts a string length and copies it to the clipboard. Microsoft Scripting Guy, Ed Wilson, is here. It is hot and humid outside&mdash;89 degrees Fahrenheit (32 degrees Celsius) and 75% humidity. That was the great thing about Madrid. Whereas, it was [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3261","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=3261"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3261\/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=3261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}