{"id":17921,"date":"2010-06-28T00:01:00","date_gmt":"2010-06-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/06\/28\/hey-scripting-guy-how-can-i-use-windows-powershell-to-create-a-text-file-with-fixed-length-lines\/"},"modified":"2010-06-28T00:01:00","modified_gmt":"2010-06-28T00:01:00","slug":"hey-scripting-guy-how-can-i-use-windows-powershell-to-create-a-text-file-with-fixed-length-lines","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-use-windows-powershell-to-create-a-text-file-with-fixed-length-lines\/","title":{"rendered":"Hey, Scripting Guy! How Can I Use Windows PowerShell to Create a Text File with Fixed-Length Lines?"},"content":{"rendered":"<p><a href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\">   &nbsp;<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/p>\n<p>&nbsp;\n<img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\">\nHey, Scripting Guy! I am trying to learn Windows PowerShell 2.0, but I am having some issues when it comes to string manipulation. I have been a faithful reader of the Scripting Guys for several years, and I have found many of the scripts to be critical in my business environment. I wish I could simply translate the VBScript scripts we use directly into Windows PowerShell and not have to fool with learning the &ldquo;PowerShell way of doing things.&rdquo; Why should I have to learn a whole new way of working with strings and files? I mean, if a Windows PowerShell script works, is that not the &ldquo;PowerShell way?&rdquo; It seems to me like you are letting a bunch of UNIX guys try to tell me how to administer my Windows servers.\n&#8212; RS\n&nbsp;\n<img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">\nHello RS,\nMicrosoft Scripting Guy Ed Wilson here. One of the great things about being a Microsoft Scripting Guy is the ability to talk and to interact with customers. I always learn from these interactions. Sometimes I learn that I have not been clear about a particular topic, or I learn that a feature I was not too impressed with has solved a particular need for someone. Many times I learn new techniques, tricks, or tips that make me a better scripter.\nI am the first to admit I do not know everything there is to know about VBScript, WMI, ADSI, the .NET Framework, or Windows PowerShell. The topics are too big, and I do not believe that anyone knows everything about scripting. The truly great thing about Windows PowerShell is that you do not need to know everything about Windows PowerShell to write a script that works. This is also both a cool thing and a frustrating thing.\nLet me explain a little bit. Many times I spend hours working on a script only to have someone pop up and say, &ldquo;Why didn&rsquo;t you do such and such?&rdquo; Quite often, their suggestion takes my 30-line script and shortens it down to a single line of code. Does this mean my script is &ldquo;wrong&rdquo; and theirs is &ldquo;right&rdquo;? Not at all. If a script works, it works. If a script solves your problem, it has done its job.\nCan a script be made better? In nearly every case the answer is yes. What is the best script? Well, that depends on your criteria. Here are some common criteria for judging scripts:<\/p>\n<ul>\n<li>Shortest amount of code <\/li>\n<li>Shortest amount of development time <\/li>\n<li>Most robust (ability to run in many different environments) <\/li>\n<li>Easiest to read <\/li>\n<li>Most maintainable <\/li>\n<li>Easiest to troubleshoot <\/li>\n<li>Most reusable <\/li>\n<li>Easiest to use <\/li>\n<\/ul>\n<p>As you can see, the many ways to judge the best script are almost as varied as the number of different people writing scripts. To directly answer your question, in most cases you can in fact make a Windows PowerShell script seem like a VBScript. If this works for you, by all means go for it. The thing is that in many cases Windows PowerShell has the ability to make things much easier. You mentioned you do a lot of work with strings. Let&rsquo;s take as an example a Hey, Scripting Guy! Blog post you may remember from the past: &ldquo;How Can I Make Sure That No Line in a Text File Exceeds 261 Characters?&rdquo; The VBScript from that article is shown here.\n<strong>ShortenLinesInTextFile.vbs<\/strong>\n<\/p>\n<div class=\"code\"><font color=\"#000000\">Const<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">ForReading<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#800000\">1<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#000000\">Const<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">ForWriting<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#800000\">2<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#0000ff\">Set<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">objFSO<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">CreateObject(<\/font><font color=\"#808080\">&#8220;Scripting.FileSystemObject&#8221;<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#0000ff\">Set<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">objFile<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">objFSO.OpenTextFile<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">(<\/font><font color=\"#808080\">&#8220;C:HSGhsg62810.txt&#8221;<\/font><font color=\"#000000\">,<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">ForReading)<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#000000\">Do<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">Until<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">objFile.AtEndOfStream<\/font><font color=\"#808080\">&nbsp; <br \/>&nbsp;&nbsp;&nbsp; <\/font><font color=\"#000000\">strLine<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">objFile.ReadLine<\/font><font color=\"#808080\">&nbsp; <br \/>&nbsp;&nbsp;&nbsp; <\/font><font color=\"#000000\">strLine<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">Left(strLine,<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#800000\">9<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">&nbsp; <br \/>&nbsp;&nbsp;&nbsp; <\/font><font color=\"#000000\">strContents<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">strContents<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">&amp;<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">strLine<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">&amp;<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">vbCrLf<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#000000\">Loop<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#000000\">objFile.Close<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#0000ff\">Set<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">objFile<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">objFSO.OpenTextFile(<\/font><font color=\"#808080\">&#8220;c:scriptstest.txt&#8221;<\/font><font color=\"#000000\">,<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">ForWriting)<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#000000\">objFile.<\/font><font color=\"#0000ff\">Write<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">strContents<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#000000\">objFile.Close<\/font> <\/div>\n<p>The script is designed to read a text file that contains lines of varying lengths, and shorten the lines to a specific length (9 characters, in this example). If the line is shorter than 9 characters, that is okay and there is no requirement for line padding. The text file in question is shown in the following image.\n<a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/june\/hey0628\/hsg-06-28-10-01.jpg\"><img decoding=\"async\" title=\"\" alt=\"Image of text file in question\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/june\/hey0628\/hsg-06-28-10-01.jpg\"><\/a>\nAfter the script has run, the text file shown in the following image is created.\n<a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/june\/hey0628\/hsg-06-28-10-01.jpg\"><img decoding=\"async\" title=\"\" alt=\"Image of text file created after script is run\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/june\/hey0628\/hsg-06-28-10-01.jpg\"><\/a>\nTo translate this script to Windows PowerShell requires very little effort. The key things to remember are that in Windows PowerShell new objects are created by the <b>New-Object<\/b> cmdlet, and that variables have a dollar sign in front of them. Oh yeah, the <b>Do&hellip;Until<\/b> loop does not have a <b>loop<\/b> command. One of the key tricks to making a translated VBScript is getting the ability to use the VBScript functions. Windows PowerShell does not have a <b>Left<\/b> command that is used for string manipulation. It has a <b>substring<\/b> method from the .NET Framework <b>string<\/b> class. To gain access to the VBScript <b>string<\/b> functions, load the <b>microsoft.visualbasic<\/b> assembly by using the <b>Add-Type<\/b> cmdlet (in Windows PowerShell 1.0, you used the <b>reflections.assembly<\/b> .NET Framework class). To make using <b>Left<\/b> easier, create an alias for the <b>microsoft.visualbasic.strings<\/b> .NET Framework class. These two commands are shown here:\n<\/p>\n<div class=\"code\"><font color=\"#000000\">add-type<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">-AssemblyName<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">microsoft.visualbasic<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#2b91af\">$strings<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\"> &#8220;microsoft.visualbasic.strings&#8221; <\/font><font color=\"#000000\">-as<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">[<\/font><font color=\"#0000ff\">type<\/font><font color=\"#000000\">]<\/font> <\/div>\n<p>In VBScript, there were two concatenation operators: the <b>+<\/b> and the <b>&amp;<\/b>. In Windows PowerShell, there is only the <b>+<\/b> character. In VBScript, you had the <b>VBCRLF<\/b> operator; Windows PowerShell uses <b>&ldquo;`r`n&rdquo;<\/b> to accomplish the same thing. To make the script as similar to the VBScript as possible, I created the variable <b>$VbCrLf<\/b> and set it equal to <b>&ldquo;`r`n&rdquo;<\/b> as shown here:\n<\/p>\n<div class=\"code\"><font color=\"#2b91af\">$VbCrLf<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\"> &#8220;`r`n&#8221;<\/font> <\/div>\n<p>The complete ShortenLinesInTextFileTransFmVBS.ps1 script is shown here.\n<strong>ShortenLinesInTextFileTransFmVBS.ps1<\/strong>\n<\/p>\n<div class=\"code\"><font color=\"#000000\">add-type<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">-AssemblyName<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">microsoft.visualbasic<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#2b91af\">$strings<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\"> &#8220;microsoft.visualbasic.strings&#8221; <\/font><font color=\"#000000\">-as<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">[<\/font><font color=\"#0000ff\">type<\/font><font color=\"#000000\">]<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#2b91af\">$VbCrLf<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\"> &#8220;`r`n&#8221;&nbsp; <br \/><\/font><font color=\"#2b91af\">$forReading<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#800000\">1<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#2b91af\">$forWriting<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#800000\">2<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#2b91af\">$objFSO<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">New-Object<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">-ComObject<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">scripting.filesystemobject<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#2b91af\">$objFile<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$objFSO<\/font><font color=\"#000000\">.OpenTextFile(<\/font><font color=\"#808080\">&#8220;C:HSGhsg62810.txt&#8221;<\/font><font color=\"#000000\">,<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$forReading<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#000000\">Do<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#000000\">{<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#2b91af\">$strLine<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$objFile<\/font><font color=\"#000000\">.ReadLine()<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#2b91af\">$strLine<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$strings<\/font><font color=\"#000000\">::Left(<\/font><font color=\"#2b91af\">$strLine<\/font><font color=\"#000000\">,<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#800000\">9<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#2b91af\">$strContents<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$strContents<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">+<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$strLine<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">+<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$VbCrLf<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#000000\">}<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">Until(<\/font><font color=\"#2b91af\">$objFile<\/font><font color=\"#000000\">.AtEndOfStream)<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#2b91af\">$objFile<\/font><font color=\"#000000\">.Close()<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#2b91af\">$objFile<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$objFSO<\/font><font color=\"#000000\">.OpenTextFile(<\/font><font color=\"#808080\">&#8220;C:HSGhsg62810.txt&#8221;<\/font><font color=\"#000000\">,<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$forWriting<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#2b91af\">$objFile<\/font><font color=\"#000000\">.<\/font><font color=\"#0000ff\">Write<\/font><font color=\"#000000\">(<\/font><font color=\"#2b91af\">$strContents<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#2b91af\">$objFile<\/font><font color=\"#000000\">.Close()<\/font> <\/div>\n<p>Another way to write this script that takes advantage of some of the features of Windows PowerShell is seen in the CreateFileWithLinesOfSpecificLength.ps1 script. I decided to add one improvement to the script, and that is to make a backup copy of the file before modifying the original text file. After assigning the path to the file, I use the <b>Rename-Item<\/b> cmdlet to rename the original file to hsg62810.txt.old. To do that, I take advantage of the expanding string feature in Windows PowerShell. When a variable is placed inside double quotation marks, it automatically expands to display the value that is contained inside the variable. This makes it really easy to do things such as append new file extensions to original file names. This is shown here inside the Windows PowerShell console where the path is assigned to the <b>$file<\/b> variable, and then the file with a new extension is displayed. Note that this does not assign a new value to the <b>$file<\/b> variable, but just displays a string with .old appended to it. This is shown here:\n<\/p>\n<div class=\"code\"><font color=\"#0000ff\">PS<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">C:&gt;<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$file<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\"> &#8220;C:fsohsg62810.txt&#8221;      <\/p>\n<p><\/font><font color=\"#0000ff\">PS<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">C:&gt;<\/font><font color=\"#808080\"> &#8220;$file.old&#8221;      <\/p>\n<p><\/font><font color=\"#000000\">C:fsohsg62810.txt.old<\/font><font color=\"#808080\">      <\/p>\n<p><\/font><font color=\"#0000ff\">PS<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">C:&gt;<\/font> <\/div>\n<p>To read the contents of a text file, I like to use the <b>Get-Content<\/b> cmdlet. Because I renamed the original file to hsg62810.txt.old, I use the string <b>&ldquo;$file.old&rdquo;<\/b> as the path to the file. The <b>Get-Content<\/b> cmdlet returns an array of strings. This array of strings is sent down the pipeline to the <b>Foreach-Object<\/b> cmdlet. In the <b>Foreach-Object<\/b> cmdlet, the <b>if<\/b> statement is used to check the length of each line as it comes down the pipeline. The <b>$_<\/b> automatic variable is used as a placeholder in the <b>Foreach-Object<\/b> cmdlet to allow us to work with the lines of text as they come down the pipeline. Each line of text is an instance of the <b>system.string<\/b> .NET Framework class, which has a <b>length<\/b> property that indicates how many letters are in the string. If the length is less than or equal to (<b>-le<\/b> operator) 9 characters, the line of text is added to the newly created hsg2810.txt file. The new file will be automatically created by the <b>Add-Content<\/b> cmdlet. The text that is written to the file is the string represented by the <b>$_<\/b> variable. This section of the script is shown here:\n<\/p>\n<div class=\"code\"><font color=\"#0000ff\">ForEach-Object<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">{<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#0000ff\">if<\/font><font color=\"#000000\">(<\/font><font color=\"#2b91af\">$_<\/font><font color=\"#000000\">.length<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">-le<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#800000\">9<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">{<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">add-content<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">-Path<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$file<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">-Value<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$_<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">}<\/font> <\/div>\n<p>If the line of text is longer than 9 characters, the <b>system.string<\/b> <b>substring<\/b> method is used to return the first 9 characters from the line. There are two parameters for the <b>substring<\/b> method. The first one is the starting position, and the second parameter is the number of characters to return. This is very similar to the VBScript function <b>mid<\/b>. An example of using the VBScript function <b>mid<\/b> is shown here:\n<\/p>\n<div class=\"code\"><font color=\"#000000\">Dim<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">MyVar<\/font><font color=\"#808080\">      <\/p>\n<p><\/font><font color=\"#000000\">MyVar<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">Mid(<\/font><font color=\"#808080\">&#8220;VB Script is fun!&#8221;<\/font><font color=\"#000000\">,<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#800000\">4<\/font><font color=\"#000000\">,<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#800000\">6<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\"> &#8216; MyVar contains &#8220;Script&#8221;.      <br \/><\/font><\/div>\n<p>The same <b>substring<\/b> command is shown here. You will notice that Windows PowerShell begins numbering at 0 for the starting position:<\/p>\n<div class=\"code\"><font color=\"#0000ff\">PS<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">C:&gt;<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$myvar<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\"> &#8220;VB Script is fun!&#8221;      <\/p>\n<p><\/font><font color=\"#0000ff\">PS<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">C:&gt;<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$myvar<\/font><font color=\"#000000\">.Substring(<\/font><font color=\"#800000\">4<\/font><font color=\"#000000\">,<\/font><font color=\"#800000\">6<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">      <\/p>\n<p><\/font><font color=\"#000000\">Script<\/font><font color=\"#808080\">      <\/p>\n<p><\/font><font color=\"#0000ff\">PS<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">C:&gt;<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$myvar<\/font><font color=\"#000000\">.Substring(<\/font><font color=\"#800000\">3<\/font><font color=\"#000000\">,<\/font><font color=\"#800000\">6<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">      <\/p>\n<p><\/font><font color=\"#000000\">Script<\/font><font color=\"#808080\">      <\/p>\n<p><\/font><font color=\"#0000ff\">PS<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">C:&gt;<\/font><font color=\"#808080\">      <br \/><\/font><\/div>\n<p>The cool thing is that because everything in Windows PowerShell is an object, a string is in reality already an instance of the <b>system.string<\/b> .NET Framework class. This means the <b>substring<\/b> method is immediately available on any string. You do not even have to put parentheses around the string first. This is shown here:<\/p>\n<div class=\"code\"><font color=\"#0000ff\">PS<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">C:&gt;<\/font><font color=\"#808080\"> &#8220;VB Script is fun!&#8221;<\/font><font color=\"#000000\">.substring(<\/font><font color=\"#800000\">3<\/font><font color=\"#000000\">,<\/font><font color=\"#800000\">6<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">      <\/p>\n<p><\/font><font color=\"#000000\">Script<\/font><font color=\"#808080\">      <\/p>\n<p><\/font><font color=\"#0000ff\">PS<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">C:&gt;<\/font> <\/div>\n<p>The <b>Add-Content<\/b> cmdlet automatically appends to a text file, and there is no need to add any special parameters to cause this action. The line from the script that selects nine characters from the beginning of the line of text on the pipeline and writes it to the text file is shown here:\n<\/p>\n<div class=\"code\"><font color=\"#000000\">{<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">Add-Content<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">-Path<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$file<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">-Value<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$_<\/font><font color=\"#000000\">.Substring(<\/font><font color=\"#800000\">0<\/font><font color=\"#000000\">,<\/font><font color=\"#800000\">9<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">}<\/font> <\/div>\n<p>The complete CreateFileWithLinesOfSpecificLength.ps1 script is shown here.\n<strong>CreateFileWithLinesOfSpecificLength.ps1<\/strong>\n<\/p>\n<div class=\"code\"><font color=\"#2b91af\">$file<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">=<\/font><font color=\"#808080\"> &#8220;C:fsohsg62810.txt&#8221;&nbsp; <br \/><\/font><font color=\"#0000ff\">Rename-Item<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">-Path<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$file<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">-NewName<\/font><font color=\"#808080\"> &#8220;$file.old&#8221;&nbsp; <br \/><\/font><font color=\"#0000ff\">Get-Content<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">-Path<\/font><font color=\"#808080\"> &#8220;$file.old&#8221; <\/font><font color=\"#000000\">|<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#0000ff\">ForEach-Object<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">{<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#0000ff\">if<\/font><font color=\"#000000\">(<\/font><font color=\"#2b91af\">$_<\/font><font color=\"#000000\">.length<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">-le<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#800000\">9<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">{<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">add-content<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">-Path<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$file<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">-Value<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$_<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">}<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#0000ff\">Else<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#000000\">{<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#0000ff\">Add-Content<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">-Path<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$file<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">-Value<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#2b91af\">$_<\/font><font color=\"#000000\">.Substring(<\/font><font color=\"#800000\">0<\/font><font color=\"#000000\">,<\/font><font color=\"#800000\">9<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">&nbsp;<\/font><font color=\"#000000\">}<\/font><font color=\"#808080\">&nbsp; <br \/><\/font><font color=\"#000000\">}<\/font> <\/div>\n<p>RS, that is all there is to using Windows PowerShell to create a file with fixed-length lines. String Migration Week will continue tomorrow.\nIf you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<br \/>\n<b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b>\n<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I am trying to learn Windows PowerShell 2.0, but I am having some issues when it comes to string manipulation. I have been a faithful reader of the Scripting Guys for several years, and I have found many of the scripts to be critical in my business environment. I wish [&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,155,45],"class_list":["post-17921","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-vbscript-migration","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I am trying to learn Windows PowerShell 2.0, but I am having some issues when it comes to string manipulation. I have been a faithful reader of the Scripting Guys for several years, and I have found many of the scripts to be critical in my business environment. I wish [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17921","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=17921"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17921\/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=17921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}