# Array: Oject reference not set

**URL:** <https://forums.powershell.org/t/array-oject-reference-not-set/24795>\
**Category:** PowerShell Help\
**Created:** [September 24, 2024, 9:06pm UTC](https://forums.powershell.org/t/array-oject-reference-not-set/24795 "2024-09-24T21:06:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PowershellUserName](https://avatars.discourse-cdn.com/v4/letter/p/838e76/32.png) [@PowershellUserName](https://forums.powershell.org/u/PowershellUserName)\
**Post date:** [September 24, 2024, 9:06pm UTC](https://forums.powershell.org/t/array-oject-reference-not-set/24795/1 "2024-09-24T21:06:08Z")

</div>

I am a newbie to PowerShell. I am working with Windows 10/11. I have been looking for a way to remove hidden serial ports. These can be seen in Device Manager if “Show Hidden” is enabled.

I found this script online “removeGhosts.ps1”. It can be found at:

> <https://github.com/istvans/scripts/blob/master/removeGhosts.ps1>

This script will list and delete the “hidden” devices. However, I want to be more selective with the delete, deleting only USB-RS232 dongles.

When I run the script, the results are put into a PS array. However, I cannot access the array with an index. I get the error:  
“out-lineoutput : Object reference not set to an instance of an object”.

I watched several YouTube videos. They give examples by populating the array with strings and then accessing the strings with an index, aka: MyArray[1]. That seems to be a simpler case than what I have.

I read some forum entries that talk about this error but either it didn’t work in my situation or I did not understand it, probably the latter. 😉

Using PowerShell ISE, here are some commands from my PowerShell session.

##############################  
$Devices = .\removeGhosts.ps1 -listGhostDevicesOnly -narrowByClass Ports  
NarrowByClass: Ports  
List ghost devices without removal: True  
Total ghost devices found : 114  
Total filtered ghost devices found : 12  
##############################  
$Devices.GetType()  
IsPublic IsSerial Name BaseType  
-------- -------- ---- -------- True True Object System.Array  
##############################  
$Devices.Count  
16  
##############################  
$Devices.Length  
16  
##############################  
$Devices.IsFixedSize  
True  
##############################  
$Devices

FriendlyName HWID InstallState Class

* * *

Prolific USB-to-Serial Comm Port (COM6) USB\VID\_067B&PID\_2303&REV\_0400 False Ports  
Silicon Labs CP210x USB to UART Bridge (COM23) USB\VID\_10C4&PID\_EA60&REV\_0100 False Ports  
USB Serial Port (COM11) FTDIBUS\COMPORT&VID\_0403&PID\_6015 False Ports  
USB Serial Port (COM12) FTDIBUS\COMPORT&VID\_0403&PID\_6015 False Ports  
USB Serial Port (COM20) FTDIBUS\COMPORT&VID\_0403&PID\_6015 False Ports  
USB Serial Port (COM21) FTDIBUS\COMPORT&VID\_0403&PID\_6015 False Ports  
USB Serial Port (COM22) FTDIBUS\COMPORT&VID\_0403&PID\_6015 False Ports  
USB Serial Port (COM4) FTDIBUS\COMPORT&VID\_0403&PID\_6015 False Ports  
XDS110 Class Application/User UART (COM10) USB\VID\_0451&PID\_BEF3&REV\_0100&MI\_00 False Ports  
XDS110 Class Application/User UART (COM13) USB\VID\_0451&PID\_BEF3&REV\_0100&MI\_00 False Ports  
XDS110 Class Auxiliary Data Port (COM14) USB\VID\_0451&PID\_BEF3&REV\_0100&MI\_03 False Ports  
XDS110 Class Auxiliary Data Port (COM9) USB\VID\_0451&PID\_BEF3&REV\_0100&MI\_03 False Ports  
##############################  
$Devices[1]  
out-lineoutput : Object reference not set to an instance of an object.  
+ CategoryInfo : NotSpecified: (🙂 [out-lineoutput], NullReferenceException  
+ FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.OutLineOutputCommand

##############################  
So, what I am hoping to do is iterate through the list to remove the “hidden” USB-to-RS232 serial ports. I do not want to remove the XDS110 units or any other device that might not be a USB/232 DB9 dongle. I guess I will have to search for: (“USB Serial Port”|Prolific|CP210x). Of course, I will have to expand that list as others become known. I think the first step is to be able to index through the array but I cannot figure it out.

Any help will be appreciated.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [September 25, 2024, 8:01am UTC](https://forums.powershell.org/t/array-oject-reference-not-set/24795/2 "2024-09-25T08:01:53Z")

</div>

The issue is the author ruined the output by including formatting commands in the function. This is easily discovered by simply inspecting the output with `Get-Member`

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/2/2e052ad95f45d2f00a78953b12ef18bda79525e6.png)

I’ve corrected the script by removing the calls to Format-Table (alias ft) and you can find it here

> <https://gist.github.com/krzydoug/e598e02ddcbf586730ce6b4851269095>

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [October 25, 2024, 8:02am UTC](https://forums.powershell.org/t/array-oject-reference-not-set/24795/3 "2024-10-25T08:02:43Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
