problem with powershell nesting for each loop

First of all: Use proper indentation. It will make it easier for you to see where nested code blocks start and end.

Does this do what you want?

$dir = "$Filelocation\Inbox_Subfolders"

Get-ChildItem $dir -Filter *.txt | foreach {
    $folder = $_.Name.Split("_")[2]
    $TextToAdd = "#" + $folder + "#"
    (Get-Content $_.FullName) | foreach {
        $_ + $TextToAdd
    } | Set-Content $_.FullName

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top