Make good use of ForEach

Make good usage of ForEach

This blog is about make good use of ForEach. Effectively Using ForEach in PowerShell is a powerful way to iterate through collections of objects and perform actions on each item. Whether you’re working with arrays, hash tables, or other collections, ForEach offers a flexible and efficient method to optimize your scripts.

Basisprincipes van ForEach

Using ForEach starts with understanding the fundamentals. In PowerShell, you can use ForEach either as a cmdlet (ForEach-Object) or as a keyword within a script block. Both approaches have their own benefits and use cases.

ForEach-Object Cmdlet

Using the ForEach-Object cmdlet is useful when working with pipeline objects. It allows you to perform an action on each object that flows through the pipeline. For example:

Get-Process | ForEach-Object { $_.Name }

In this example, we use ForEach-Object to retrieve the name of each process.

ForEach Keyword

Using the ForEach keyword is ideal for iterating through a collection within a script block. It offers more control and flexibility. For example:

$processes = Get-Process
ForEach ($process in $processes) {
    Write-Output $process.Name
}

Here, we use the ForEach keyword to iterate through the collection of processes and display their names.

Make good use of ForEach for advanced scenarios

Using ForEach in PowerShell goes beyond simple iterations. You can implement complex logic, such as conditional statements and nested loops, to perform advanced tasks. For example:

$files = Get-ChildItem -Path "C:\Temp"
ForEach ($file in $files) {
    If ($file.Extension -eq ".txt") {
        Write-Output "Tekstbestand gevonden: $($file.Name)"
    }
}

In this example, we use ForEach to identify text files in a directory and display a message.

How to Effectively Use ForEach in Real-World Applications

Using ForEach in PowerShell is common when performing a minor upgrade of Business Central. The Product DVD often contains dozens of apps that a customer may not need. These can include test modules, language modules, library modules, and more.

Conclusion make good use of ForEach

Using ForEach in PowerShell is essential for automating tasks and processing data. By understanding the different methods and applications, you can make your scripts more efficient and powerful.

The following code loops through a path provided as a variable and filters for all files with the .app extension. Once that’s done, it filters out the unwanted extensions based on specific criteria.

$extensionFiles = Get-ChildItem -Path $extensionsPath -Recurse -Filter *.app
# Loop through each extension file and copy it to the destination folder
foreach ($extensionFile in $extensionFiles) {
    Copy-Item -Path $extensionFile.FullName -Exclude *Test*, *Tests*.app,*_Tests_*.app, *_Test.app, *_Test_*.app, *Test.app     -Destination $extensionToBeInstalled
}

The following script publishes the desired extensions.

$extensionFiles = Get-ChildItem -Path $extensionsToBeInstalled -Recurse -Filter *.app
# Loop through each extension file and publish it
foreach ($extensionFile in $extensionFiles) {
    Publish-NAVApp -ServerInstance $BcServerInstance -Path $extensionFile.FullName -SkipVerification
}

The first and second script could, of course, have been combined, but in a real-world scenario, you naturally want to first verify whether everything was properly filtered out. In the example above, the language packs were not filtered out, so this customer ended up with Italian and Danish language packs on their system. It remains a task that still requires human oversight!

More information about PowerShell can be found on the Microsoft site. More information about the author can be found on the Digitale Mels site.

0 Shares:
You May Also Like