Array brackets [] with an offset value starting at 0 returns error

I use sqlserver psdrive, then I cd to databases and issue the following

$a = Dir | ?{$_.Schema -eq "$schemaname" } |  ft Name, RowCount, DataSpaceUsed, IndexSpaceUsed -Auto

Typing $a I get 4 or 5 records. checked the $a type and I get

$a.GetType()
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

So It is an array.
Trying to acces 1st item using $a[0] I get nothing, doing $a[1], I get an error

> Object reference not set to an instance of an object.
>     + CategoryInfo          : NotSpecified: (:) [out-lineoutput], NullReferenceException
>     + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.OutLineOutputCommand

What am I doing wrong? Thanks

Don’t pipe to a Format-* cmdlet unless you’re outputting the data.

Also, avoid aliases in scripts (and in forum posts :slight_smile: ) because they’re harder to read and debug.

1 Like

OK great, yes I removed the formatting and now the array is working fine. Do you mean aliases like “ft”?

Yes, ? and ft.

PowerShell Practice and Style Guide - Naming Conventions