Share via

PowerShell script help

Anonymous
2025-02-10T16:47:55+00:00

Hello,

I am trying to use PowerShell to edit similar file names (examples below).

no_25!2-2_FORSE_COMP.prn

no_25!2-4_FORSE_COMP.prn

no_25!2-9_FORSE_COMP.prn

no_30!10-5_FORSE_COMP.prn

gb_10!01-_5_FORSE_COMP.prn

no_30!11-3_FORSE_COMP.prn

I need all the writing in upper case, the '!' changed to '_' and the file extension changed to '.txt'

What would be the simplest way to do this? I managed to change the name for the ones beginning with the same text up til the '!', using the following:

PS F:\FILES> Get-ChildItem -Filter no_25!*.* | Rename-Item -NewName {$_.name -replace 'no_25!', 'NO_25_' } - **** but would there be a simpler way to do this for all the files? (And also to change the extension to .txt)

Thank you!

Windows for home | Windows 10 | Files, folders, and storage

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
  1. Anonymous
    2025-02-10T17:22:00+00:00

    Hi, I'm Ravish, an independent Windows advisor, and I'm here to assist you today.

    To efficiently rename all files while making the desired changes, you can use a single PowerShell script. Below is a streamlined and flexible solution:

    # Directory where your files are located
    $directory = "F:\FILES"
    
    # Get all files with the '.prn' extension
    Get-ChildItem -Path $directory -Filter *.prn | ForEach-Object {
        $newName = $_.Name.ToUpper()                # Convert the entire name to uppercase
        $newName = $newName -replace '!', '_'        # Replace '!' with '_'
        $newName = [System.IO.Path]::ChangeExtension($newName, ".txt") # Change extension to .txt
    
        Rename-Item -Path $_.FullName -NewName $newName
    }
    

    Instructions

    Replace "F:\FILES" with your target folder path.

    Save the script in a .ps1 file or run it directly in PowerShell.

    Ensure your PowerShell execution policy allows script running. If not, use:

    Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
    

    This approach will handle multiple variations and apply all requested changes efficiently.

    Hope it helps

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2025-02-12T14:29:04+00:00

    One more question, how would I edit the script to remove a specific part of the file name from multiple files? For example, if I wanted to remove 'FORSE_COMP' from all? Thanks!

    0 comments No comments
  2. Anonymous
    2025-02-10T18:23:45+00:00

    Welcome and have a great day.

    0 comments No comments
  3. Anonymous
    2025-02-10T17:49:46+00:00

    Thank you very much, this has done exactly as I needed :)

    0 comments No comments