Remove multiple headings / Descriptions /Title from Powershell output

chirag darji 136 Reputation points
2023-06-12T07:23:08.7033333+00:00

Hi,

Below is my VMWare tagging assignment script. I have seen that the Description/Title is repeating every time when I set Format-Table -AutoSize

clear
$error.clear()
$FormatEnumerationLimit = 8
#Get-Tag | Export-Csv -Path C:\temp\gettaglist.csv -NoTypeInformation
$importtaglist = Import-Csv -Path C:\temp\gettaglist.csv
$newtaglist = Import-Csv -Path C:\temp\newtaglist.csv
$taglistname = @()
$taglistcategory = @()
$output =@()
for($i=0; $i -lt $importtaglist.Count; $i++)
{
    $taglistname += $importtaglist[$i].Name
    $taglistcategory += $importtaglist[$i].Category
}
foreach($tags in $newtaglist)
{
    $taglist = $tags.Name
    $tagcategory = $tags.Category
    
    if(($taglistname -contains $taglist) -and ($taglistcategory -contains $tagcategory))
    {
       $output += [PSCustomObject]@{
                    NewTagName = $taglist;
                    ExistsTagList = "Match";
                    NewCategory=$tagcategory; 
                    ExistsCategory=$tagcategory
                    } | ft -AutoSize
       
    }
    else
    {
       
       $output += [PSCustomObject]@{
                    NewTagName=$taglist;
                    ExistsTagName="Not Match"; 
                    NewCategory=$tagcategory; 
                    ExistsCategory="Not Match"
                    } | ft -AutoSize
                    
      }
     
         
}
$output

I need it Below output instead of multiple headings / Descriptions /Title from

Require Output

User's image

Result output.

User's image

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 36,291 Reputation points
    2023-06-13T12:04:07.4866667+00:00

    Ahhhh, I see it now. Your objects have different properties. One has ExistsTagList and the other has ExistsTagName.

    If the column should be ExistsTagName then change ExistsTagList to ExistsTagName.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2023-06-12T12:33:23.61+00:00

    Don't pipe each custom object to format-table. Just add it to the output variable.

        $output += [PSCustomObject]@{
                        NewTagName=$taglist;
                        ExistsTagName="Not Match"; 
                        NewCategory=$tagcategory; 
                        ExistsCategory="Not Match"
                        }
    
    
    

    After all the data has been gathered, pipe the entire $output variable to format-table -autosize.


  2. Rich Matheisen 47,901 Reputation points
    2023-06-12T15:52:28.4733333+00:00

    Is it important that name and category in both data sets match? In other words, that the name in one list and its associated value, match the name and category in the other? Because your code only requires that the name and category are simply present in the 1st list. In a very simple example, you might have this condition:

    List 1:
    Name Category
    A    One
    B    Two
    
    List 2:
    Name Category
    A    Two
    B    One
    

    . . . and the script will say they match!

    Here's another way to rewrite the script and, if the script should be verifying that the Name/Category pairs match in both lists, it's a simple matter to modify the code to accomplish that.

    Clear-Host
    $error.clear()
    
    #Get-Tag | Export-Csv -Path C:\temp\gettaglist.csv -NoTypeInformation
    
    # Create two hashes instead of two arrays (faster lookups)
    # only the keys are necessary, leave the values as empty strings
    $taglistname = @{}
    $taglistcategory = @{}
    
    Import-Csv -Path C:\temp\gettaglist.csv|
        ForEach-Object{
            $taglistname[$_.Name] = ""
            $taglistcategory[$_.Category] = ""
        }
    
    Import-Csv -Path C:\temp\newtaglist.csv |
        ForEach-Object{
            $match = "Not Match"
            if ($taglistname.ContainsKey($_.Name) -AND $taglistcategory.ContainsKey($_.Category){
                $match = "Match"
            }
            [PSCustomObject]@{
                NewTagName = $_.Name
                ExistsTagName = $Match
                NewCategory=$_.Category
                ExistsCategory=$_.Category
            }
        }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.