PowerShell: Script for Loop through Files and Folders

In this article, we will look at examples of using constructions to loop over all files and folders on a disk or in a specific directory that you can widely use in your PowerShell scripts.

Usually, the task of iterating over all file system objects in a directory arises when you need to perform a certain action with all nested objects. For example, you need to delete, copy, move files, add or replace lines in all files in the specific directory by some criteria.

To get a list of child objects (folders and files) in a directory, use the Get-ChildItem PowerShell cmdlet. This is the most popular file system cmdlet. There are several aliases for ChildItem: gci, dir, ls.

The Get-ChildItem cmdlet displays a list of child (nested) objects on a specified drive or directory. The path to the directory is specified through the –Path attribute.

For example, to list the files in the C:\PS directory, run the command:

Get-ChildItem -Path ‘C:\PS’

powershell script for loop

However, it displays a list of objects located in the root of the specified directory. You can also display the contents of child directories using the –Recurse parameter:

Get-ChildItem -Path ‘C:\PS’ –Recurse

As you can see, the contents of each subdirectory are displayed sequentially.

powershell script loop through files

Now, let’s look at the general structure of the ForEach loop when using the Get-ChildItem cmdlet.

Get-ChildItem –Path "C:\PS" |

Foreach-Object {

#Do something with $_.FullName


Also you can use such a loop structure (but we like it less):

READ ALSO  How to Find Active Directory User's/Computer's Last Logon Time?
foreach($file in Get-ChildItem $SomeFolder)
# Do something

For example, you can delete all files with the *.log extension in the specified directory and all subfolders (we won’t really delete the files from disk by adding the parameter WhatIF):

Get-ChildItem –Path "C:\PS\" -Recurse -Filter *.log


Foreach-Object {

Remove-Item $_.FullName -WhatIF


The script found 3 files with the extension log and indicated that they could be deleted by this script.

Consider a script that deletes files older than 10 days in a directory (it can be used when you need to clean up the logs folder, or public network folders).

Get-ChildItem C:\ps -Recurse |

Where-Object { $_.CreationTime -lt ($(Get-Date).AddDays(-10))} |

ForEach-Object { remove-Item $_.FullName –whatif }

The following file loop example allows for all *.log files in the directory to find files containing the text ‘flush_log’ or ‘error’ and saves the found lines to files with a new extension (_outlog):

$files = Get-ChildItem C:\ps -Recurse *.log

foreach ($f in $files){

$outfile = $f.FullName + "_outlog"

Get-Content $f.FullName | Where-Object { ($_ -match 'flush_log' -or $_ -match 'error') } | Set-Content $outfile


Such a PowerShell script can be useful when searching for specific event entries in log files and filtering out all that is unnecessary.

Cyril Kardashevsky
Latest posts by Cyril Kardashevsky (see all)
READ ALSO  How to Transfer FSMO Roles Using PowerShell?

One comment

  1. This was a great help. Had to write a basic file sync to push files newer than N to remote cloud destination. Works great!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.