File rename output

Welcome Forums General PowerShell Q&A File rename output

This topic contains 3 replies, has 4 voices, and was last updated by

js
 
Participant
1 month ago.

  • Author
    Posts
  • #113585

    Participant
    Points: 0
    Rank: Member

    Hi, I am running the script below to rename files and it is working fine. what I would like to add is something that would either show in PS or export in a CSV file all the changes made.
    For example, if I am renaming t+est.pdf to test.pdf, the output I need something that says
    old name t+est.pdf new name test.pdf.
    Set-Location -Path "C:\Temp\File rename"
    Get-ChildItem -Recurse -Include *.pdf | Rename-Item -NewName {$_.Name.Replace("+","_")}
    Thank You

  • #113603

    Participant
    Points: 135
    Helping Hand
    Rank: Participant

    You could start with something like this:

    Get-ChildItem -Path "C:\Temp\File rename" -Recurse -Include *.pdf | 
        ForEach-Object{
            [PSCustomObject]@{
                NameBefore = $_.Name
                NameAfter = $_.Name.Replace("+","_")
            }
            Rename-Item -Path $_.FullName -NewName {$_.Name.Replace("+","_")}
        }

    .... untested!!
    Of course you could pipe the output to whatever further cmdlet or file you want.

  • #113614

    Participant
    Points: 544
    Helping Hand
    Rank: Major Contributor

    You can use Write-Output to print it in the console or if in a CSV you can build a PSCustomObject with Oldname and Newname as headers and export it to a CSV. Yourself can do it, there are lot of example in the web on PSCustomObjects. Once you face any issues using CustomObjects, update it here, we will guide you.

    here is a link for understanding PScustomObjects : https://kevinmarquette.github.io/2016-10-28-powershell-everything-you-wanted-to-know-about-pscustomobject/

    If you are very new to PowerShell, I would suggest you to take some time in learning and understanding the language else you will get frustrated by doing things not right.

    A good places to start

  • #113626
    js

    Participant
    Points: 207
    Helping Hand
    Rank: Participant

    rename-item has -verbose and -passthru options. But passthru only returns in the new file.

You must be logged in to reply to this topic.