# Get-Childitem rename question

**URL:** <https://forums.powershell.org/t/get-childitem-rename-question/11777>\
**Category:** PowerShell Help\
**Created:** [December 10, 2018, 1:49pm UTC](https://forums.powershell.org/t/get-childitem-rename-question/11777 "2018-12-10T13:49:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![flyers7777](https://avatars.discourse-cdn.com/v4/letter/f/3bc359/32.png) [@flyers7777](https://forums.powershell.org/u/flyers7777)\
**Post date:** [December 10, 2018, 1:49pm UTC](https://forums.powershell.org/t/get-childitem-rename-question/11777/1 "2018-12-10T13:49:27Z")

</div>

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

[pre]

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

[/pre]

---

<div class="post-metadata">

**Author:** ![mortensummer](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mortensummer](https://forums.powershell.org/u/mortensummer)\
**Post date:** [December 10, 2018, 2:02pm UTC](https://forums.powershell.org/t/get-childitem-rename-question/11777/2 "2018-12-10T14:02:09Z")

</div>

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</pre>
> 
> ```
> ```

---

<div class="post-metadata">

**Author:** ![flyers7777](https://avatars.discourse-cdn.com/v4/letter/f/3bc359/32.png) [@flyers7777](https://forums.powershell.org/u/flyers7777)\
**Post date:** [December 10, 2018, 2:08pm UTC](https://forums.powershell.org/t/get-childitem-rename-question/11777/3 "2018-12-10T14:08:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [December 10, 2018, 2:17pm UTC](https://forums.powershell.org/t/get-childitem-rename-question/11777/4 "2018-12-10T14:17:43Z")

</div>

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 { "$_" }
```

---

<div class="post-metadata">

**Author:** ![mortensummer](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mortensummer](https://forums.powershell.org/u/mortensummer)\
**Post date:** [December 10, 2018, 2:32pm UTC](https://forums.powershell.org/t/get-childitem-rename-question/11777/5 "2018-12-10T14:32:06Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [December 10, 2018, 2:34pm UTC](https://forums.powershell.org/t/get-childitem-rename-question/11777/6 "2018-12-10T14:34:33Z")

</div>

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 { "$_" }
```

---

<div class="post-metadata">

**Author:** ![flyers7777](https://avatars.discourse-cdn.com/v4/letter/f/3bc359/32.png) [@flyers7777](https://forums.powershell.org/u/flyers7777)\
**Post date:** [December 11, 2018, 7:57am UTC](https://forums.powershell.org/t/get-childitem-rename-question/11777/7 "2018-12-11T07:57:08Z")

</div>

Thanks js appreciate everyone help on this.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/get-childitem-rename-question/11777/8 "2024-05-16T20:34:07Z")

</div>

Closed on May 16, 2024, 8:34pm UTC


