Replacing 2nd occurrence of “-” with “_” using Powershell










2















I've been using Powershell to simplify repetitive tasks in creating directories, renaming and moving files. I'm working with Video and PDF files where the required syntax of the filename is very specific. So far I've been able to correct all of the common errors I run across, but this one has me stumped.



The correct syntax for my files would include:



01A-50_02A-50-CIPP-PRE.MP4
01AA-50_02AA-50-CIPP-PNSL.PDF
W01AA-48_02AA-48-CIPP-PST-CMP.MPG


I'm receiving a large amount of files that look like this:



01A-50-02A-50-CIPP-PRE.MP4
01AA-50-02AA-50-CIPP-PNSL.PDF
W01AA-48-02AA-48-CIPP-PST-CMP.MPG


I need to replace the second dash with an underscore while keeping the other dashes unaffected. I can otherwise do this in bulk with some help from excel, but I was hoping to have a short code that could find and correct this error in syntax without having to export a list to excel, use text to columns, and then concatenate the alphanumeric portions back together. I also don't want to correct all these filenames manually.



From what I've researched, it's not possible to target a specific occurrence of a character to replace. the closest I've come to thinking I'd found a solution involved REGEX and identifying and replacing a pattern. I haven't been able to do anything constructive with that.



The way I would use this code would be by opening the folder that contains the missnamed files, opening a Powershell window there, copying the code from a txt file on my desktop, and pasting it into Powershell.



Any help on this would be greatly appreciated.










share|improve this question




























    2















    I've been using Powershell to simplify repetitive tasks in creating directories, renaming and moving files. I'm working with Video and PDF files where the required syntax of the filename is very specific. So far I've been able to correct all of the common errors I run across, but this one has me stumped.



    The correct syntax for my files would include:



    01A-50_02A-50-CIPP-PRE.MP4
    01AA-50_02AA-50-CIPP-PNSL.PDF
    W01AA-48_02AA-48-CIPP-PST-CMP.MPG


    I'm receiving a large amount of files that look like this:



    01A-50-02A-50-CIPP-PRE.MP4
    01AA-50-02AA-50-CIPP-PNSL.PDF
    W01AA-48-02AA-48-CIPP-PST-CMP.MPG


    I need to replace the second dash with an underscore while keeping the other dashes unaffected. I can otherwise do this in bulk with some help from excel, but I was hoping to have a short code that could find and correct this error in syntax without having to export a list to excel, use text to columns, and then concatenate the alphanumeric portions back together. I also don't want to correct all these filenames manually.



    From what I've researched, it's not possible to target a specific occurrence of a character to replace. the closest I've come to thinking I'd found a solution involved REGEX and identifying and replacing a pattern. I haven't been able to do anything constructive with that.



    The way I would use this code would be by opening the folder that contains the missnamed files, opening a Powershell window there, copying the code from a txt file on my desktop, and pasting it into Powershell.



    Any help on this would be greatly appreciated.










    share|improve this question


























      2












      2








      2








      I've been using Powershell to simplify repetitive tasks in creating directories, renaming and moving files. I'm working with Video and PDF files where the required syntax of the filename is very specific. So far I've been able to correct all of the common errors I run across, but this one has me stumped.



      The correct syntax for my files would include:



      01A-50_02A-50-CIPP-PRE.MP4
      01AA-50_02AA-50-CIPP-PNSL.PDF
      W01AA-48_02AA-48-CIPP-PST-CMP.MPG


      I'm receiving a large amount of files that look like this:



      01A-50-02A-50-CIPP-PRE.MP4
      01AA-50-02AA-50-CIPP-PNSL.PDF
      W01AA-48-02AA-48-CIPP-PST-CMP.MPG


      I need to replace the second dash with an underscore while keeping the other dashes unaffected. I can otherwise do this in bulk with some help from excel, but I was hoping to have a short code that could find and correct this error in syntax without having to export a list to excel, use text to columns, and then concatenate the alphanumeric portions back together. I also don't want to correct all these filenames manually.



      From what I've researched, it's not possible to target a specific occurrence of a character to replace. the closest I've come to thinking I'd found a solution involved REGEX and identifying and replacing a pattern. I haven't been able to do anything constructive with that.



      The way I would use this code would be by opening the folder that contains the missnamed files, opening a Powershell window there, copying the code from a txt file on my desktop, and pasting it into Powershell.



      Any help on this would be greatly appreciated.










      share|improve this question
















      I've been using Powershell to simplify repetitive tasks in creating directories, renaming and moving files. I'm working with Video and PDF files where the required syntax of the filename is very specific. So far I've been able to correct all of the common errors I run across, but this one has me stumped.



      The correct syntax for my files would include:



      01A-50_02A-50-CIPP-PRE.MP4
      01AA-50_02AA-50-CIPP-PNSL.PDF
      W01AA-48_02AA-48-CIPP-PST-CMP.MPG


      I'm receiving a large amount of files that look like this:



      01A-50-02A-50-CIPP-PRE.MP4
      01AA-50-02AA-50-CIPP-PNSL.PDF
      W01AA-48-02AA-48-CIPP-PST-CMP.MPG


      I need to replace the second dash with an underscore while keeping the other dashes unaffected. I can otherwise do this in bulk with some help from excel, but I was hoping to have a short code that could find and correct this error in syntax without having to export a list to excel, use text to columns, and then concatenate the alphanumeric portions back together. I also don't want to correct all these filenames manually.



      From what I've researched, it's not possible to target a specific occurrence of a character to replace. the closest I've come to thinking I'd found a solution involved REGEX and identifying and replacing a pattern. I haven't been able to do anything constructive with that.



      The way I would use this code would be by opening the folder that contains the missnamed files, opening a Powershell window there, copying the code from a txt file on my desktop, and pasting it into Powershell.



      Any help on this would be greatly appreciated.







      regex powershell replace renaming






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 15:11









      mklement0

      135k21252289




      135k21252289










      asked Nov 15 '18 at 22:31









      J_BRASJ_BRAS

      234




      234






















          5 Answers
          5






          active

          oldest

          votes


















          4














          Use the -replace operator with a regular expression:



          Get-ChildItem |
          Rename-Item -NewName $_.Name -replace '^([^_-]+-[^_-]+)-', '$1_' -WhatIf


          -WhatIf previews the renaming operation; remove it to perform actual renaming.




          • Regex '^([^_-]+-[^_-]+)-' captures the first two - separated tokens at the start (^) of the filename, using a capture group ((...)) to capture the tokens excluding the 2nd -.




            • [^_-]+ captures any nonempty run of characters that are neither - nor _. _ is also ruled out in order to prevent false positives with filenames that already are correct; for those, not ruling out _ would match the first 3 tokens and insert an additional _ there.


          • Replacement operand $1_ then uses the value of the 1st (and only) capture group ($1) followed by a literal _ to replace what the regex matched, which effectively replaces the 2nd - with a _.


          • If a given file name doesn't match the regex (if it already is correct), the name is returned as-is, which is a quiet no-op in the context of Rename-Item.






          share|improve this answer
































            1














            Looking at your examples, it appears that the second - always appears between numbers. Something like $Variable -replace 'REGEX','_'



            Using the below regex will match those.



            (?<=[0-9])(.)(?=[0-9])


            () creates a group to match, it is a capturing group.



            ?<= is a positive lookbehind, it matches a group before the main expression without including it in the results



            [0-9] is the character set, matching anything between 0 and 9.



            . matches any character except line breaks



            ?= is a positive lookahead, it matches a group after the main expression without including it in the results



            I recommend using Regexr to test and learn Regex.






            share|improve this answer























            • unfortunately, not always between numbers. sometimes like this E21U-50A_E21U-50-CIPP-PST-CMP

              – J_BRAS
              Nov 16 '18 at 15:05











            • thanks for the breakdown. very helpful

              – J_BRAS
              Nov 16 '18 at 15:16


















            1














            You could split the string on the first two occurrences of -, then concatenate them by - and _:



            $name = '01A-50-02A-50-CIPP-PRE.MP4'
            $first,$second,$rest = $name -split '-',3
            $newName = "$first-$second_$rest"





            share|improve this answer


















            • 1





              for some reason, '0-1_2' -f $first, $second, $rest seems nicer to me. [grin]

              – Lee_Dailey
              Nov 16 '18 at 1:10


















            1














            What about this RegEx: (?<=(^|n)[^-]*-[^-]*)-?



            Or as a full command (using answers to Replace Part of File Name Powershell):



            Get-ChildItem | Rename-Item -NewName $_.name -replace '(?<=^[^-_]+-[^-_]+)-','_'


            Edit: incorporated suggestions from @mklement0






            share|improve this answer

























            • Nailed it! This was what I was trying to do.

              – J_BRAS
              Nov 16 '18 at 15:11


















            1














            Thank you Solomon Ucko!
            That was almost exactly what I was looking for.



            Get-ChildItem | Rename-Item -NewName $.name -replace '(?<=(^



            It worked great on all of the examples I could throw at it, EXCEPT...
            if I ran the code on a mixed group of misnamed and properly named files, it would add another underscore where it didn't belong...



            "E21U-50A_E21U_50-CIPP-PST-CMP"


            instead of



            "E21U-50A_E21U-50-CIPP-PST-CMP"


            the fix for that was easy enough.

            All I did was replace all _s with -s, first.



            Get-ChildItem | Rename-Item -NewName $_.name -replace '_','-'

            Get-ChildItem | Rename-Item -NewName $_.name -replace '(?<=(^


            Thanks to everyone who had other ideas. Admittedly I have not tried them because this solution was the first one I tried and it did the trick.

            I am, however, going to tinker with the other solutions after I get my work done.

            Thanks again.






            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328793%2freplacing-2nd-occurrence-of-with-using-powershell%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              5 Answers
              5






              active

              oldest

              votes








              5 Answers
              5






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              4














              Use the -replace operator with a regular expression:



              Get-ChildItem |
              Rename-Item -NewName $_.Name -replace '^([^_-]+-[^_-]+)-', '$1_' -WhatIf


              -WhatIf previews the renaming operation; remove it to perform actual renaming.




              • Regex '^([^_-]+-[^_-]+)-' captures the first two - separated tokens at the start (^) of the filename, using a capture group ((...)) to capture the tokens excluding the 2nd -.




                • [^_-]+ captures any nonempty run of characters that are neither - nor _. _ is also ruled out in order to prevent false positives with filenames that already are correct; for those, not ruling out _ would match the first 3 tokens and insert an additional _ there.


              • Replacement operand $1_ then uses the value of the 1st (and only) capture group ($1) followed by a literal _ to replace what the regex matched, which effectively replaces the 2nd - with a _.


              • If a given file name doesn't match the regex (if it already is correct), the name is returned as-is, which is a quiet no-op in the context of Rename-Item.






              share|improve this answer





























                4














                Use the -replace operator with a regular expression:



                Get-ChildItem |
                Rename-Item -NewName $_.Name -replace '^([^_-]+-[^_-]+)-', '$1_' -WhatIf


                -WhatIf previews the renaming operation; remove it to perform actual renaming.




                • Regex '^([^_-]+-[^_-]+)-' captures the first two - separated tokens at the start (^) of the filename, using a capture group ((...)) to capture the tokens excluding the 2nd -.




                  • [^_-]+ captures any nonempty run of characters that are neither - nor _. _ is also ruled out in order to prevent false positives with filenames that already are correct; for those, not ruling out _ would match the first 3 tokens and insert an additional _ there.


                • Replacement operand $1_ then uses the value of the 1st (and only) capture group ($1) followed by a literal _ to replace what the regex matched, which effectively replaces the 2nd - with a _.


                • If a given file name doesn't match the regex (if it already is correct), the name is returned as-is, which is a quiet no-op in the context of Rename-Item.






                share|improve this answer



























                  4












                  4








                  4







                  Use the -replace operator with a regular expression:



                  Get-ChildItem |
                  Rename-Item -NewName $_.Name -replace '^([^_-]+-[^_-]+)-', '$1_' -WhatIf


                  -WhatIf previews the renaming operation; remove it to perform actual renaming.




                  • Regex '^([^_-]+-[^_-]+)-' captures the first two - separated tokens at the start (^) of the filename, using a capture group ((...)) to capture the tokens excluding the 2nd -.




                    • [^_-]+ captures any nonempty run of characters that are neither - nor _. _ is also ruled out in order to prevent false positives with filenames that already are correct; for those, not ruling out _ would match the first 3 tokens and insert an additional _ there.


                  • Replacement operand $1_ then uses the value of the 1st (and only) capture group ($1) followed by a literal _ to replace what the regex matched, which effectively replaces the 2nd - with a _.


                  • If a given file name doesn't match the regex (if it already is correct), the name is returned as-is, which is a quiet no-op in the context of Rename-Item.






                  share|improve this answer















                  Use the -replace operator with a regular expression:



                  Get-ChildItem |
                  Rename-Item -NewName $_.Name -replace '^([^_-]+-[^_-]+)-', '$1_' -WhatIf


                  -WhatIf previews the renaming operation; remove it to perform actual renaming.




                  • Regex '^([^_-]+-[^_-]+)-' captures the first two - separated tokens at the start (^) of the filename, using a capture group ((...)) to capture the tokens excluding the 2nd -.




                    • [^_-]+ captures any nonempty run of characters that are neither - nor _. _ is also ruled out in order to prevent false positives with filenames that already are correct; for those, not ruling out _ would match the first 3 tokens and insert an additional _ there.


                  • Replacement operand $1_ then uses the value of the 1st (and only) capture group ($1) followed by a literal _ to replace what the regex matched, which effectively replaces the 2nd - with a _.


                  • If a given file name doesn't match the regex (if it already is correct), the name is returned as-is, which is a quiet no-op in the context of Rename-Item.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 16 '18 at 19:21

























                  answered Nov 15 '18 at 23:00









                  mklement0mklement0

                  135k21252289




                  135k21252289























                      1














                      Looking at your examples, it appears that the second - always appears between numbers. Something like $Variable -replace 'REGEX','_'



                      Using the below regex will match those.



                      (?<=[0-9])(.)(?=[0-9])


                      () creates a group to match, it is a capturing group.



                      ?<= is a positive lookbehind, it matches a group before the main expression without including it in the results



                      [0-9] is the character set, matching anything between 0 and 9.



                      . matches any character except line breaks



                      ?= is a positive lookahead, it matches a group after the main expression without including it in the results



                      I recommend using Regexr to test and learn Regex.






                      share|improve this answer























                      • unfortunately, not always between numbers. sometimes like this E21U-50A_E21U-50-CIPP-PST-CMP

                        – J_BRAS
                        Nov 16 '18 at 15:05











                      • thanks for the breakdown. very helpful

                        – J_BRAS
                        Nov 16 '18 at 15:16















                      1














                      Looking at your examples, it appears that the second - always appears between numbers. Something like $Variable -replace 'REGEX','_'



                      Using the below regex will match those.



                      (?<=[0-9])(.)(?=[0-9])


                      () creates a group to match, it is a capturing group.



                      ?<= is a positive lookbehind, it matches a group before the main expression without including it in the results



                      [0-9] is the character set, matching anything between 0 and 9.



                      . matches any character except line breaks



                      ?= is a positive lookahead, it matches a group after the main expression without including it in the results



                      I recommend using Regexr to test and learn Regex.






                      share|improve this answer























                      • unfortunately, not always between numbers. sometimes like this E21U-50A_E21U-50-CIPP-PST-CMP

                        – J_BRAS
                        Nov 16 '18 at 15:05











                      • thanks for the breakdown. very helpful

                        – J_BRAS
                        Nov 16 '18 at 15:16













                      1












                      1








                      1







                      Looking at your examples, it appears that the second - always appears between numbers. Something like $Variable -replace 'REGEX','_'



                      Using the below regex will match those.



                      (?<=[0-9])(.)(?=[0-9])


                      () creates a group to match, it is a capturing group.



                      ?<= is a positive lookbehind, it matches a group before the main expression without including it in the results



                      [0-9] is the character set, matching anything between 0 and 9.



                      . matches any character except line breaks



                      ?= is a positive lookahead, it matches a group after the main expression without including it in the results



                      I recommend using Regexr to test and learn Regex.






                      share|improve this answer













                      Looking at your examples, it appears that the second - always appears between numbers. Something like $Variable -replace 'REGEX','_'



                      Using the below regex will match those.



                      (?<=[0-9])(.)(?=[0-9])


                      () creates a group to match, it is a capturing group.



                      ?<= is a positive lookbehind, it matches a group before the main expression without including it in the results



                      [0-9] is the character set, matching anything between 0 and 9.



                      . matches any character except line breaks



                      ?= is a positive lookahead, it matches a group after the main expression without including it in the results



                      I recommend using Regexr to test and learn Regex.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 15 '18 at 22:41









                      DrewDrew

                      1,417418




                      1,417418












                      • unfortunately, not always between numbers. sometimes like this E21U-50A_E21U-50-CIPP-PST-CMP

                        – J_BRAS
                        Nov 16 '18 at 15:05











                      • thanks for the breakdown. very helpful

                        – J_BRAS
                        Nov 16 '18 at 15:16

















                      • unfortunately, not always between numbers. sometimes like this E21U-50A_E21U-50-CIPP-PST-CMP

                        – J_BRAS
                        Nov 16 '18 at 15:05











                      • thanks for the breakdown. very helpful

                        – J_BRAS
                        Nov 16 '18 at 15:16
















                      unfortunately, not always between numbers. sometimes like this E21U-50A_E21U-50-CIPP-PST-CMP

                      – J_BRAS
                      Nov 16 '18 at 15:05





                      unfortunately, not always between numbers. sometimes like this E21U-50A_E21U-50-CIPP-PST-CMP

                      – J_BRAS
                      Nov 16 '18 at 15:05













                      thanks for the breakdown. very helpful

                      – J_BRAS
                      Nov 16 '18 at 15:16





                      thanks for the breakdown. very helpful

                      – J_BRAS
                      Nov 16 '18 at 15:16











                      1














                      You could split the string on the first two occurrences of -, then concatenate them by - and _:



                      $name = '01A-50-02A-50-CIPP-PRE.MP4'
                      $first,$second,$rest = $name -split '-',3
                      $newName = "$first-$second_$rest"





                      share|improve this answer


















                      • 1





                        for some reason, '0-1_2' -f $first, $second, $rest seems nicer to me. [grin]

                        – Lee_Dailey
                        Nov 16 '18 at 1:10















                      1














                      You could split the string on the first two occurrences of -, then concatenate them by - and _:



                      $name = '01A-50-02A-50-CIPP-PRE.MP4'
                      $first,$second,$rest = $name -split '-',3
                      $newName = "$first-$second_$rest"





                      share|improve this answer


















                      • 1





                        for some reason, '0-1_2' -f $first, $second, $rest seems nicer to me. [grin]

                        – Lee_Dailey
                        Nov 16 '18 at 1:10













                      1












                      1








                      1







                      You could split the string on the first two occurrences of -, then concatenate them by - and _:



                      $name = '01A-50-02A-50-CIPP-PRE.MP4'
                      $first,$second,$rest = $name -split '-',3
                      $newName = "$first-$second_$rest"





                      share|improve this answer













                      You could split the string on the first two occurrences of -, then concatenate them by - and _:



                      $name = '01A-50-02A-50-CIPP-PRE.MP4'
                      $first,$second,$rest = $name -split '-',3
                      $newName = "$first-$second_$rest"






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 15 '18 at 22:59









                      Mathias R. JessenMathias R. Jessen

                      58.4k463109




                      58.4k463109







                      • 1





                        for some reason, '0-1_2' -f $first, $second, $rest seems nicer to me. [grin]

                        – Lee_Dailey
                        Nov 16 '18 at 1:10












                      • 1





                        for some reason, '0-1_2' -f $first, $second, $rest seems nicer to me. [grin]

                        – Lee_Dailey
                        Nov 16 '18 at 1:10







                      1




                      1





                      for some reason, '0-1_2' -f $first, $second, $rest seems nicer to me. [grin]

                      – Lee_Dailey
                      Nov 16 '18 at 1:10





                      for some reason, '0-1_2' -f $first, $second, $rest seems nicer to me. [grin]

                      – Lee_Dailey
                      Nov 16 '18 at 1:10











                      1














                      What about this RegEx: (?<=(^|n)[^-]*-[^-]*)-?



                      Or as a full command (using answers to Replace Part of File Name Powershell):



                      Get-ChildItem | Rename-Item -NewName $_.name -replace '(?<=^[^-_]+-[^-_]+)-','_'


                      Edit: incorporated suggestions from @mklement0






                      share|improve this answer

























                      • Nailed it! This was what I was trying to do.

                        – J_BRAS
                        Nov 16 '18 at 15:11















                      1














                      What about this RegEx: (?<=(^|n)[^-]*-[^-]*)-?



                      Or as a full command (using answers to Replace Part of File Name Powershell):



                      Get-ChildItem | Rename-Item -NewName $_.name -replace '(?<=^[^-_]+-[^-_]+)-','_'


                      Edit: incorporated suggestions from @mklement0






                      share|improve this answer

























                      • Nailed it! This was what I was trying to do.

                        – J_BRAS
                        Nov 16 '18 at 15:11













                      1












                      1








                      1







                      What about this RegEx: (?<=(^|n)[^-]*-[^-]*)-?



                      Or as a full command (using answers to Replace Part of File Name Powershell):



                      Get-ChildItem | Rename-Item -NewName $_.name -replace '(?<=^[^-_]+-[^-_]+)-','_'


                      Edit: incorporated suggestions from @mklement0






                      share|improve this answer















                      What about this RegEx: (?<=(^|n)[^-]*-[^-]*)-?



                      Or as a full command (using answers to Replace Part of File Name Powershell):



                      Get-ChildItem | Rename-Item -NewName $_.name -replace '(?<=^[^-_]+-[^-_]+)-','_'


                      Edit: incorporated suggestions from @mklement0







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 16 '18 at 18:21

























                      answered Nov 15 '18 at 22:51









                      Solomon UckoSolomon Ucko

                      7612822




                      7612822












                      • Nailed it! This was what I was trying to do.

                        – J_BRAS
                        Nov 16 '18 at 15:11

















                      • Nailed it! This was what I was trying to do.

                        – J_BRAS
                        Nov 16 '18 at 15:11
















                      Nailed it! This was what I was trying to do.

                      – J_BRAS
                      Nov 16 '18 at 15:11





                      Nailed it! This was what I was trying to do.

                      – J_BRAS
                      Nov 16 '18 at 15:11











                      1














                      Thank you Solomon Ucko!
                      That was almost exactly what I was looking for.



                      Get-ChildItem | Rename-Item -NewName $.name -replace '(?<=(^



                      It worked great on all of the examples I could throw at it, EXCEPT...
                      if I ran the code on a mixed group of misnamed and properly named files, it would add another underscore where it didn't belong...



                      "E21U-50A_E21U_50-CIPP-PST-CMP"


                      instead of



                      "E21U-50A_E21U-50-CIPP-PST-CMP"


                      the fix for that was easy enough.

                      All I did was replace all _s with -s, first.



                      Get-ChildItem | Rename-Item -NewName $_.name -replace '_','-'

                      Get-ChildItem | Rename-Item -NewName $_.name -replace '(?<=(^


                      Thanks to everyone who had other ideas. Admittedly I have not tried them because this solution was the first one I tried and it did the trick.

                      I am, however, going to tinker with the other solutions after I get my work done.

                      Thanks again.






                      share|improve this answer





























                        1














                        Thank you Solomon Ucko!
                        That was almost exactly what I was looking for.



                        Get-ChildItem | Rename-Item -NewName $.name -replace '(?<=(^



                        It worked great on all of the examples I could throw at it, EXCEPT...
                        if I ran the code on a mixed group of misnamed and properly named files, it would add another underscore where it didn't belong...



                        "E21U-50A_E21U_50-CIPP-PST-CMP"


                        instead of



                        "E21U-50A_E21U-50-CIPP-PST-CMP"


                        the fix for that was easy enough.

                        All I did was replace all _s with -s, first.



                        Get-ChildItem | Rename-Item -NewName $_.name -replace '_','-'

                        Get-ChildItem | Rename-Item -NewName $_.name -replace '(?<=(^


                        Thanks to everyone who had other ideas. Admittedly I have not tried them because this solution was the first one I tried and it did the trick.

                        I am, however, going to tinker with the other solutions after I get my work done.

                        Thanks again.






                        share|improve this answer



























                          1












                          1








                          1







                          Thank you Solomon Ucko!
                          That was almost exactly what I was looking for.



                          Get-ChildItem | Rename-Item -NewName $.name -replace '(?<=(^



                          It worked great on all of the examples I could throw at it, EXCEPT...
                          if I ran the code on a mixed group of misnamed and properly named files, it would add another underscore where it didn't belong...



                          "E21U-50A_E21U_50-CIPP-PST-CMP"


                          instead of



                          "E21U-50A_E21U-50-CIPP-PST-CMP"


                          the fix for that was easy enough.

                          All I did was replace all _s with -s, first.



                          Get-ChildItem | Rename-Item -NewName $_.name -replace '_','-'

                          Get-ChildItem | Rename-Item -NewName $_.name -replace '(?<=(^


                          Thanks to everyone who had other ideas. Admittedly I have not tried them because this solution was the first one I tried and it did the trick.

                          I am, however, going to tinker with the other solutions after I get my work done.

                          Thanks again.






                          share|improve this answer















                          Thank you Solomon Ucko!
                          That was almost exactly what I was looking for.



                          Get-ChildItem | Rename-Item -NewName $.name -replace '(?<=(^



                          It worked great on all of the examples I could throw at it, EXCEPT...
                          if I ran the code on a mixed group of misnamed and properly named files, it would add another underscore where it didn't belong...



                          "E21U-50A_E21U_50-CIPP-PST-CMP"


                          instead of



                          "E21U-50A_E21U-50-CIPP-PST-CMP"


                          the fix for that was easy enough.

                          All I did was replace all _s with -s, first.



                          Get-ChildItem | Rename-Item -NewName $_.name -replace '_','-'

                          Get-ChildItem | Rename-Item -NewName $_.name -replace '(?<=(^


                          Thanks to everyone who had other ideas. Admittedly I have not tried them because this solution was the first one I tried and it did the trick.

                          I am, however, going to tinker with the other solutions after I get my work done.

                          Thanks again.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 16 '18 at 21:18









                          mklement0

                          135k21252289




                          135k21252289










                          answered Nov 16 '18 at 14:53









                          J_BRASJ_BRAS

                          234




                          234



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328793%2freplacing-2nd-occurrence-of-with-using-powershell%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Top Tejano songwriter Luis Silva dead of heart attack at 64

                              政党

                              天津地下鉄3号線