powershell – remove a blank line from the variable hash property

First thing to point out is your variable $sql_servers does not contain hashtables, but rather PSCustomObjects with one property. For this specific scenario you could remove empty entries by adjusting your command to

$sql_servers = $sql_servers | Where-Object {$_.machinename}

If it were hashtables, you could use

$sql_servers = $sql_servers | Where-Object {$_.values}

Here is a simple demonstration of both.

PSObject

1..5 | % {
    if($_ % 2 -eq 0)
    {
        $num = $_
    }
    else
    {
        $num = $null
    }
    [PSCustomObject]@{
        MachineName = $num
    }
} -ov sql_servers

MachineName
-----------
       
2          
       
4          


$sql_servers | ? {$_.machinename} -ov sql_servers

MachineName
-----------
          2
          4   

Hashtable

1..5 | % {
    if($_ % 2 -eq 0)
    {
        $num = $_
    }
    else
    {
        $num = $null
    }
    @{
        MachineName = $num
    }
} -ov sql_servers

Name                           Value                                                                                                                            
----                           -----                                                                                                                            
MachineName                                                                                                                                                     
MachineName                    2                                                                                                                                
MachineName                                                                                                                                                     
MachineName                    4                                                                                                                                
MachineName                                                                                                                                                     


$sql_servers | ? {$_.values} -ov sql_servers

Name                           Value                                                                                                                            
----                           -----                                                                                                                            
MachineName                    2                                                                                                                                
MachineName                    4   

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top