Get-Childitem rename question

Welcome Forums General PowerShell Q&A Get-Childitem rename question

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

 
Participant
1 month, 1 week ago.

  • Author
    Posts
  • #129093

    Participant
    Points: 20
    Rank: Member

    Hello,

    I am trying to have a function search for all the files of a specific name and then rename them.  I have been able to make this work if I navigate to the directory first but if I dont I get an error saying the file does not exist.  I just wanted to see if this just how powershell works and you have to be in the correct directory?  Thanks.

    #Working Code only if in the directory

    
    Function Test{
    $count = ([int]1)
    Set-Location C:\Powershell\Testing\Pictures
    $DefaultFiles = Get-ChildItem -Recurse | Where-Object {$_.Name -like "IMG_0*"}
    foreach ($file in $DefaultFiles){
    $newname = ([string]$file).Replace("IMG_", "Worked_$count")
    $count = $count + 1
    Rename-Item -Path $file -NewName $newname
    
    }#end of foreach
    
    }#end of Test function
    
    
  • #129099

    Participant
    Points: 167
    Helping Hand
    Rank: Participant

    It's to do with your $File variable.

    When renaming, you need to pass the full name and path into the rename-item function, thus the $file.fullname.

    When debugging and learning , i drop in "write-output" to help me understand what variables equate to:

        foreach ($file in $DefaultFiles){
            $newname = ([string]$file.Name).Replace("IMG_", "Worked_$count")
            Write-Output $file.fullname
            Write-output $newname
    
            $count = $count + 1
            Rename-Item -Path $file.fullname -NewName $newname -WhatIf
        }#end of foreach
    • #129101

      Participant
      Points: 20
      Rank: Member

      Thanks so much Tom,  that worked perfectly had no idea about .fullname.  I am definatly going to start putting more write-host in my code in the future to catch things like that.  Thanks again.

  • #129105
    js

    Participant
    Points: 452
    Helping Hand
    Rank: Contributor

    It's an annoyance in powershell 5. Converting the output of get-childitem to a string doesn't give the full path. in powershell 6 you get the full path.

    get-childitem | foreach-object { "$_" }
    
  • #129110

    Participant
    Points: 167
    Helping Hand
    Rank: Participant

    Nice.. i didnt know that!

    I need to get using PS6 properly for all my day to day scripting... hopefully there will be more useful improvements like this!

    • #129113
      js

      Participant
      Points: 452
      Helping Hand
      Rank: Contributor

      There's still missing cmdlets in ps 6 (like get-netfirewallrule). But there's lots of little fixes like that. Here's another workaround:

      get-childitem | get-item | foreach-object { "$_" } 
      
    • #129188

      Participant
      Points: 20
      Rank: Member

      Thanks js appreciate everyone help on this.

You must be logged in to reply to this topic.