Export lines that contain a specific value to a CSV file with PowerShell

For a client I needed to find all the hostnames that where defined in each IBM iSeries Client Access Workstation file (.ws file). All the .ws files where in subfolders. The country, the filename and the hostname needs to be in a .csv file.

The directory structure:

Folder PATH listing for volume Windows
Volume serial number is 000000BE 
C:.
|   
\---Sessions
    +---CAT Austria
    |       CAT_01.WS
    |       CAT_02.WS
    |       
    +---CBE Belgium
    |       CBE_01.ws
    |       CBE_02.ws
    |       
    +---CCH Switzerland
    |       CCH_01.ws
    |       CCH_02.ws
    |       CCH_03.ws
    |       
    \---Generic sessions
            GEN_01_xx.ws
            GEN_02_xx.WS
            GEN_03_xx.ws
            GEN_TEST_xx.WS
            GEN_UAT.WS

And the content of a .ws file:

[Profile]
ID=WS
[Communication]
HostName=HOST653.company.local

As I want to learn PowerShell, I decided to use PowerShell.

Here is my script:

###########################################################
# FILE:     GetHostnamesFromWSFiles_v100.ps1   
#
# AUTHOR:   Willem-Jan Vroom
# DATE:     17-March-2017
# COMMENT:  This script reads all the hostnames as defined
#           in the .ws files
###########################################################

# ---------------------------------------------------------
# Define the variables
# ---------------------------------------------------------

$Directory        = "$PSScriptRoot\*"
$CSVFile          = "$PSScriptRoot\Results.csv"
$arrWithWSFiles   = Get-ChildItem -Path $Directory -Include *.ws -Recurse
$arrResults       = @()

# ---------------------------------------------------------
# Process each .ws file and search for hostname
# If found, add to the array $arrResults:
#    Country:  The foldername that contains the .ws file
#    Filename: The filename from the .ws file
#    Hostname: The entry that begins with hostname= and
#              contains the AS400 host name to connect to.
# ---------------------------------------------------------

ForEach ($WSFileName in $arrWithWSFiles)
{
 $content = Get-Content $WSFileName
 ForEach ($line in $content)
  {
  $line=$line.tolower()
  $Position = $line.IndexOf("hostname")
  if ($Position -eq 0)
     {
     $DetailedLine               = "" | Select Country,FileName,AS400Hostname
     $CountryCode                = ""
     $CountryCode                = $WSFileName.DirectoryName
     $LastPosition               = $CountryCode.LastIndexOfAny("\")
     $DetailedLine.Country       = $CountryCode.SubString($LastPosition+1, $CountryCode.Length - ($LastPosition + 1))
     $DetailedLine.FileName      = $WSFileName.Name
     $DetailedLine.AS400Hostname = $line -replace("hostname=","")
     $arrResults += $DetailedLine     
     } 
  } 
}

# ---------------------------------------------------------
# Export the array with the results to a CSV File.
# ---------------------------------------------------------
 
$arrResults | Export-CSV -Path $CSVFile

And in this case the output looks like:

#TYPE Selected.System.String
"Country","FileName","AS400Hostname"
"CAT Austria","CAT_01.WS","host653.company.local"
"CAT Austria","CAT_02.WS","host007.company.local"
"CBE Belgium","CBE_01.ws","host123.company.local"
"CBE Belgium","CBE_02.ws","host481.company.local"
"CCH Switzerland","CCH_01.ws","host053.company.local"
"CCH Switzerland","CCH_02.ws","host613.company.local"
"CCH Switzerland","CCH_03.ws","host653.company.local"
"Generic sessions","GEN_01_xx.ws","host001.company.local"
"Generic sessions","GEN_02_xx.WS","host002.company.local"
"Generic sessions","GEN_03_xx.ws","host003.company.local"
"Generic sessions","GEN_TEST_xx.WS","hosttest.company.local"
"Generic sessions","GEN_UAT.WS","hostuat.company.local"

The script can be found here.