PowerShell Loop Through Files

Migel Hewage Nimesha Feb 03, 2022
PowerShell Loop Through Files

Windows PowerShell runs on Command Line Instructions to provide flexibility similar to Unix-like command-line-based systems to Windows, Mac, and Linux users.

PowerShell is the solution that enables Scripting, Automation and includes a Configuration Management framework. PowerShell can be considered as a Scripting solution that enables cross-platform automation facilities.

PowerShell was developed and published by the Microsoft Cooperation. PowerShell comes installed in all the Windows versions from Windows 7.

However, if it is a macOS Computer, you have to download and install the PowerShell version suitable for your macOS version.

The automation functionality of PowerShell is useful in automating complex tasks in environments like Servers, where there are complex files like system log files to be maintained, handled, compared, and analyzed.

PowerShell supports handling these log files by looping through them, tailing the files to wait for changes in the files, comparing different files to understand changes, and so on.

We will focus on looping through files in a directory through his article. When we loop through a set of files, we can compare the files, find specific things in those files and edit the files.

We are working on a Windows 10 machine to do the following. You can try on your version to see the same outputs.

Use the Get-ChildItem and get-Content to Loop Through and Retrieve the Content of the Log Files

There are large directories of the log files which have to be gone through and edited for different requirements of System Administrators.

Therefore, we will discuss a generic method of looping through the files in this context. A Foreach-Object shall use to loop through the items.

What we are trying to do is a bit complicated.

  • First, Loop through the list of files, filtering by the type of file.txt files are taken for testing purposes. A Foreach-Object for loop is used to loop through the files.
  • Then, we will look for files that contain AA. A screenshot of the file we used as input is included below in fig.1.
  • The Get-Content is used to get the content of the files of the filtered type.
  • Then, all the files go through to find the matching lines.

We want to delete all lines in the file that do not contain AA.

  • Then, through the Set-Content command, we can output the lines that contain the filtered values only.
  • A new file with the given ending is created with the filtered lines.

Here, the following image contains the input file.

Input text file used to filter

Following is the code that can use to do the above function.

 C:\Users\Agni> Get-ChildItem "C:\Users\Agni" -Filter *.txt |
>> Foreach-Object {
>>     $content = Get-Content $_.FullName
>>     $content | Where-Object {$_ -match 'AA'} | Set-Content $_.FullName
>>     $content | Where-Object {$_ -match 'AA'} | Set-Content ($_.FullName + '_out.log')
>> }

Then an output file is used to save the values that contain the filtered lines.

Following is the changed output file. Here we have to keep in mind that the original file would also change only to have the filtered values, and all the other lines would delete.

The first image here would show the original file after the command is run.

Original file after the command

The below image shows the output file created by PowerShell.

Output file after the command

Therefore, this particular command can use with different file types to perform the above task. We would need to practice using this with .log files, which contain lines of large data, which are hard to go through manually.

In that case, you can filter by .log files to get the log files in a specified location.

It is not the only way to do this task. But one of the most accepted methods is hassle-free and effective.

Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.