none
Windows 7 PowerShell cmdlet quest

    Question

  • Using web resources and books, I've struggled with Powershell to the point where I can structure and run run simple cmdlets against the files that I copy to the current directory. 

    For example.

    C:\TEMP> dir
    cal AB123 AUG.docx
    cal CDE45 AUG.docx
    cal FGHIJKL6889 AUG.docx

    C:\TEMP> Get-ChildItem | Rename-Item -NewName {$_.name -replace "cal", "calendar" }

    C:\TEMP> Get-ChildItem | Rename-Item -NewName {$_.name -replace "Aug","August" }

    C:\TEMP> dir
    calendar AB123 August.docx
    calendar CDE45 August.docx
    calendar FGHIJKL6889 August.docx

    All well and good to that point.

    Going forward, my quest is a single cmdlet that will simultaneously change specified strings (cal to calendar; AUG to August) within the filenames in the current directory while still preserving the remainder of filenames as is.  (AB123 stays AB123; CDE stays CDE45; FGHIJKL6889 stays FGHIJKL6889).

    I tried adding a wildcard but that did not work.

    C:\TEMP> Get-ChildItem | Rename-Item -NewName {$_.name -replace "cal*Aug.docx", "calendar*August.docx" }

    Any guidance will be gratefully received.

    Friday, December 2, 2016 4:49 AM

Answers

  • Hi!

    The easiest way, tested this on Windows 10, is to use this line:

    Get-ChildItem | Rename-Item -NewName {($_.name -replace "cal", "calendar") -replace "aug", "August" }

    That will change cal to calendar, keep the middle part and change aug to August.

    Best regards
    Andreas Molin


    Andreas Molin | Site: www.guidestomicrosoft.com | Twitter: andreas_molin

    • Marked as answer by pwt12855 Tuesday, January 3, 2017 6:15 AM
    Monday, January 2, 2017 9:46 AM

All replies

  • Hi!

    The easiest way, tested this on Windows 10, is to use this line:

    Get-ChildItem | Rename-Item -NewName {($_.name -replace "cal", "calendar") -replace "aug", "August" }

    That will change cal to calendar, keep the middle part and change aug to August.

    Best regards
    Andreas Molin


    Andreas Molin | Site: www.guidestomicrosoft.com | Twitter: andreas_molin

    • Marked as answer by pwt12855 Tuesday, January 3, 2017 6:15 AM
    Monday, January 2, 2017 9:46 AM
  • Thank you, Andreas Molin.  Your solution works like a charm in Windows 7.  I am so grateful that I can now get out of bang-head-here mode.
    Tuesday, January 3, 2017 6:21 AM