Search code examples
powershellformattable

Read Cells from output of Format-Table


I have 2 fields that gets pushed via Format-Table

Set-Location Cert:\LocalMachine\My
$files  = Get-ChildItem | Format-Table Subject,  Thumbprint -AutoSize

I am now trying to read those individual fields again, e.g.

Foreach ($file in $files) {    
    if($file[0] -like '*CN=*' ){
        Write-Output $file
        CallOtherMethod $file[1] 
    }
}

The above snipped does not work but inicates intent. How can one read a specific output column from the table?


Solution

  • It is already formatted data when Format-Table is called. It is not a string that you can compare. If you want to work with the data from Get-ChildItem you do not need to send it through the pipeline, or you can use Select-Object to only return chosen values.

    Set-Location Cert:\LocalMachine\My
    $files  = Get-ChildItem
    # OR
    $files  = Get-ChildItem | Select-Object Subject,Thumbprint
    
    Foreach ($file in $files) {
        # I think you meant just $file here. You should specify either the subject or thumbprint field.
        if($file.Thumbprint -like '*CN=*' ){
            Write-Output $file
            CallOtherMethod $file 
        }
    }