Comparing path variables

This posting is related to a prior posting:

# [
Add all dirPaths open in windows explorer to an array](https://forums.powershell.org/t/add-all-dirpaths-open-in-windows-explorer-to-an-array/20339)

[PowerShell Help](https://forums.powershell.org/c/powershell-qa/5)

I am trying to make a list (array) of all directories open in Windows Explorer, and to make sure the list does not include more than one (1) list item containing any particular path, i.e., no path should appear twice in the array oven though more the directory was open in more than one Windows Explorer window when the script was run. But a directory that is open twice in Windows Explorer does appear twice in the list. I’m only at Chap. 3 in Learn_PowerShell_in_a_Month_of_Lunches_4th Ed., and my ill-informed attempts at searching for an explanation on the Web have proved fruitless.

Here’s my code:

Function Get-CurrentLine {
    # https://www.jaapbrasser.com/quicktip-determine-current-line-number-in-powershell/
    $Myinvocation.ScriptlineNumber
}

Write-Host "$(Get-CurrentLine) ---------$([char]10)" 

$NuDirList = ''

$NuDirList = @(
(New-Object -ComObject 'Shell.Application').Windows() | 
    ForEach-Object  {
        $localPath = ($_.Document.Folder.Self.Path).Trim() 
        #$localPath = Convert-Path $localPath.Trim() 
        if ($NuDirList.Contains($localpath)) {
            $('$localpath is already in the list')} else 
            {
            $_.Document.Folder.Self.Path}
        }
      )
      
foreach ($localPath in $NuDirList){"Line $(Get-CurrentLine) $($localPath)-$([char]10)"}

Here’s what my code returns:

PS C:\Users\marc> E:\Apps\UtilPS\Lists_aaa.ps1
7 ---------

Line 23 E:\-

Line 23 E:\Apps\UtilPS-

Line 23 E:\-

Line 23 F:\Install\PyCharm\HelpMe\IssuesOstg-

Etc.

Any suggestions would be much appreciated.

Thanks, Marc

Leaving aside your initial declaration, $NuDirList doesn’t contain anything until the ForEach-Object loop completes so you’re checking if an empty array contains $localpath, it doesn’t so everything gets added to the list.

You have a couple of options:

Add everything to the list, then use:

$NuDirList = $NuDirList | Select-Object -Unique

Or add the items to the array in the loop:

$NuDirList = @()

(New-Object -ComObject 'Shell.Application').Windows() | 
    ForEach-Object  {
        $localPath = ($_.Document.Folder.Self.Path).Trim() 

        if ($NuDirList.Contains($localpath)) {
            "$localpath is already in the list"
        } 
        else { 
            $NuDirList += $localpath
        }
    }

Something to bear in mind as you learn PowerShell is that using += to add members to an array does not scale well and is considered a bad practice.
If we were working with large amounts of objects we would use the Generic List collection class instead.

$NuDirList = [System.Collections.Generic.List[String]]::New()

(New-Object -ComObject 'Shell.Application').Windows() | 
    ForEach-Object  {
        $localPath = ($_.Document.Folder.Self.Path).Trim() 

        if ($NuDirList.Contains($localpath)) {
            "$localpath is already in the list"
        } 
        else { 
            $NuDirList.Add($localPath)
        }
    }  
4 Likes