I have this folder structure
root\1\2\3\1.txt
root\3\2\4\6\1.txt
I want to rename text files to root_1.txt
I tried
Get-ChildItem -Recurse *.txt | Rename-Item -NewName %{$_.Parent.Parent.Name + $_.Name}
But this doesn't work because Parent work only for directories
For file items, use the Directory
property:
Get-ChildItem -Recurse *.txt | Rename-Item -NewName {$_.Directory.Parent.Name + $_.Name}