PowerShell Script to Loop through Files

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):

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.

You may also like:

How to transfer FSMO Roles From a Failed Domain Co... In case domain controller, which owns FSMO (Flexible Single Master Operation) roles, is fail (virus attack, fatal software problems or catastrophic ha...
Time Configuration for a Virtualized Domain Contro... Today we will talk about some of the features of time configuration on a virtualized domain controllers. Typically, the time synchronization scheme in...
Removing Old and Unused Drivers from Driver Store ... Each time you install or update your device driver, Windows OS (since Vista) continues to store the old version of the driver in the system Driver Sto...
Get-service: Checking the Status of Windows Servic... Using the Get-Service cmdlet you can get a list of all the services installed on the operating system, its status and startup type. This one and other...
How to Create a GUI for PowerShell Scripts? One of the significant drawbacks of PowerShell scripts, when used by users (not sysadmins or programmers), is its command-line interface. The result o...

Add Your Comment