Remove empty elements from an array

Hey, I´m kinda new into PowerShell and this is my first more complex script. What I meant is extracting informations from log - Free memory and Adapter time. Log is in .txt format and this script is actually working. But, I dont think that this is the correct way how to extract informations from some text file. Any tips for a begginer? Thanks for help

$file= 'C:\users\jhavlicek\Documents\PowerShell\hsmvypis_a.txt'

$cont= Get-Content $file


$cont| foreach {

$at = $null
$bt = $null

$a= $_| Select-String "Adapter Clock"
$b= $_| Select-String "Free Memory"

if (([string]$a).Length -ge 10)

if (([string]$b).Length -ge 10)

$array += @($at;$bt) | Where-Object {$_}


$csv= @("cas","mem")

while ($i -le $pocet) {

$newRow = New-Object PsObject -Property @{ cas = $($array[$i]) ; mem = $($array[$i+1]) }

$csv += $newRow

$csv |ft "cas","mem"

It would help us (and probably even yourself) when you fomrat your code nicely - including indentation and with less needless blank lines.
Regardless of that you could post some example lines of your log file and the expected result. This way we will not have to guess that much. :wink:

Example lines from my log are here:

ProtectToolkit C Configuration Utility 5.6.0 Copyright (c) Safenet, Inc. 2009-2017

Current Adapter Configuration for Device 0:

Model : PSI-E2:PL1500
Batch : 0000
Manufacturing date: 15/08/2018 15:52:06
Serial Number : 579000
Adapter Clock : 25/06/2019 10:57:29 (+1:00+DST)
Board Revision : 6.00
Firmware Version : 5.01.02
Cprov Version : 5.6
Hardware Status : BATTERY OK PCB v0 FPGA v0 EXT PINS 0
Free Memory : 62230528
SM Size Free/Total: 2737552/4091776
Temperature : 46 Deg C

This is example of one output from log. There are many more of those but with exact same infos. What im doing here is extracting just “Free Memory” and “Adapter Clock” into a 2 columns (One with memory, second with Clock"). Also, I get rid of names, so only value remains, for example:

25/06/2019 10:57:29 (+1:00+DST) 62230528

Hmmm … I’m still pretty unsure if I have all necessary information but this could be a starter for you … try it:

$Content = Get-Content -Path ‘C:\users\jhavlicek\Documents\PowerShell\hsmvypis_a.txt’
$Content |
Where-Object { $_ -match ‘^.+\s+:\s+.+$’ } |
ForEach-Object { $_ -replace ‘\s+:\s+’, ’ = ’ } |
ConvertFrom-StringData |
Where-Object { $.Keys -match ‘Adapter’ -or $.Keys -match ‘Memory’ }

At least with the example data you provided it should work.