Return file not found as part of object property

Have this piece of code. the aim to cycle through PC, looking PST files. would also like to indicate if no PST files found so line in CSV is not blank

there is a catch statement for when the PC is off line, sets every thing to $Null

$session = New-CimSession -ComputerName $Computer -Credential $Credential
            $PSTs = Get-CimInstance -CimSession $Session -ClassName CIM_DataFile -Filter "Drive='C:' and Extension='pst'" | Select Name, Status, LastAccessed, FileSize, InstallDate
            Foreach($PST in $PSTs){
                try{
                    $Properties = [Ordered]@{
                        'Computer Name' = $Computer
                        'Is On Line' = 'Yes'
                        'Scan Date' = $TimeStamp
                        'PST Name' = if(!($PSTs)){'NO PST FIles Found'}Else{$PST.Name}
                        'PST Size(Gb)' =  [MAth]::Round($PST.FileSize/1gb,2)
                        'PST Status' = $PST.Status
                        'PST Creation Date' = $PST.InstallDate
                        'PST Last Accessed' = $PST.LastAccessed
                    }

This line ‘PST Name’ = if(!($PSTs)){‘NO PST FIles Found’}Else{$PST.Name} doesnt work when object is empty.
Have tried if(!($PST))… same result.
the following lines code produce the correct result.
if(!(PSTs)){
write-warning “No PST files found”
}
However I would like to include in the object for output to CSV.
any assistance would be greatly appreciated
thanks in advance
Peter

If you cast the variable $PSTs to an [array] you could check for $PSTs.count -eq 0. :wink: But you should check it before your foreach loop.

1 Like

cheers. will give it a try. Thank you