Importing a list of Folder names to search a network drive using Powershell

McNaughton, John 1 Reputation point
2021-08-11T13:38:42.31+00:00

So, I am still new to writing my own scripts and am trying to search a network drive against a list of folder names for the folders that are on the list but NOT in the network drive. Because the list of names is long, I am thinking that importing a .csv is my best way forward and importing the list to the code.
This is the code I have made so far, now; it is not working (hence asking this question) but I am stumped as to if I am just completely off kilter on my script or if I am close, but it is a syntax issue of some kind. Any help to point me in the right direction is welcome.

    $Folders = Import-Csv "path for csv"

    $FolderReport =            
    foreach ($Folder in $Folders) {
        $name = $Folder.Name

        Get-Childitem -Path "Network Drive" -recurse -include {Name -eq $name}
    }

    $FolderReport |
    export-csv 'Where it's written to' -NoTypeInformation -Force

    Start 'opening the file'
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,397 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 32,091 Reputation points
    2021-08-16T17:00:49.047+00:00

    for the folders that are on the list but NOT in the network drive

    How about something like this?

    $Folders = Import-Csv "c:\temp\files.csv"
    $Found = @()                                  # Array of names that we found
    $NotFound = @()                               # Array of names that we did not find 
    
    foreach ($Folder in $Folders) {
        "Searching for {0}" -f $Folder.Name
    
         $Results=  Get-Childitem -Path "c:\temp" -recurse -include $Folder.Name
         "I found {0} objects" -f $Results.count
         # Add entry into appropriate array 
         if ($Results.count -eq 0) {
            $NotFound += [PSCustomObject]@{
                Name = $Folder.Name
            } 
         } else {
            $Found += [PSCustomObject]@{
                Name  = $Folder.Name
                Count = $Results.count
            } 
         }
    }
    
    $Found | Export-Csv c:\temp\Found.csv -NoTypeInformation            
    $NotFound | Export-Csv c:\temp\NotFound.csv -NoTypeInformation            
    
    
    "Here are the names that I found."      
    Get-Content c:\temp\Found.csv
    "Here are the names that I did not find."
    Get-Content c:\temp\NotFound.csv      
    
    0 comments No comments