Adding folder names to filenames using powershell

To ensure directory names remain unchanged use this:

get-childitem -recurse | 
  Where-Object {$_.Psiscontainer -eq $false} | 
    Rename-Item -NewName {$_.Directory.Name + "_" + $_.Name}

This will filter only the files.


To extend what @Wasif_Hasan shows, know that Get-ChildItem has built-in switch parameters for a deal with Files and Folders, that one can capture to leverage in your use case.

So, this can be refactored. Here is a select example...

Get-ChildItem -Path 'D:\Temp' -File -Filter '*.txt' -Recurse | 
ForEach-Object{$($PSItem.Directory.BaseName + '_' +  $PSItem.Name)}

...but you could do the same thing with the Rename-Item cmdlet in that ForEach loop. For example:

Get-ChildItem -Path 'D:\Temp' -File -Filter '*.txt' -Recurse | 
ForEach-Object{
    $RenameItemSplat = @{
        Path    = $PSItem.FullName 
        NewName = $PSItem.Directory.BaseName + '_' + $PSItem.Name
        WhatIf  = $true
        Confirm = $true
    }
    Rename-Item @RenameItemSplat
}

Anytime you are doing destructive code, you should check results before final execution.

# Results
<#
What if: Performing the operation "Rename File" on target "Item: D:\Temp\abc.txt Destination: D:\Temp\Temp_abc.txt".
...
What if: Performing the operation "Rename File" on target "Item: D:\Temp\AddressFiles\File1.txt Destination: D:\Temp\AddressFiles\AddressFiles_File1.txt".
...
What if: Performing the operation "Rename File" on target "Item: D:\Temp\NewFolder\awél.txt Destination: D:\Temp\NewFolder\NewFolder_awél.txt".
...
What if: Performing the operation "Rename File" on target "Item: D:\Temp\Reference\awél.txt Destination: D:\Temp\Reference\Reference_awél.txt".
...
What if: Performing the operation "Rename File" on target "Item: D:\Temp\Source\awél.txt Destination: D:\Temp\Source\Source_awél.txt".
...
#>

PowerShell Help files:

Get-ChildItem -File 'D:\Temp' 
# Results
<#
    Directory: D:\Temp


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ---- 
-a----         25-May-20     15:44             25 abc.bat
-a----         20-May-20     11:40             94 abc.txt
-a----         10-Jan-20     17:59              0 awél.txt
...
#>

Get-ChildItem -Directory 'D:\Temp'
# Results
<#
    Directory: D:\Temp


Mode          LastWriteTime Length Name        
----          ------------- ------ ----        
d-----  17-Feb-20     15:50        est         
d-----  14-Mar-20     17:03        here        
d-----  26-May-20     10:38        hold  
...
#>


# Get specifics for a module, cmdlet, or function
(Get-Command -Name Get-ChildItem).Parameters
(Get-Command -Name Get-ChildItem).Parameters.Keys
# Results
<#
...
Directory
File
...
#>
Get-help -Name Get-ChildItem -Examples
Get-help -Name Get-ChildItem -Full
# Results
<#
-Directory

To get a list of directories, use the Directory parameter or the Attributes parameter with the Directory property. You can use the Recurse parameter with Directory.

TABLE 3
Type:   SwitchParameter
Aliases:    ad, d
Position:   Named
Default value:  None
Accept pipeline input:  False
Accept wildcard characters: False



-File

To get a list of files, use the File parameter. You can use the Recurse parameter with File.

TABLE 5
Type:   SwitchParameter
Aliases:    af
Position:   Named
Default value:  None
Accept pipeline input:  False
Accept wildcard characters: False
#>
Get-help -Name Get-ChildItem -Online