< Back

Remove-ArrayObject

Fri Jan 10, 2020 8:55 pm

NAME Remove-ArrayObject



SYNOPSIS

Remove the specified object(s) from within your existing array.





SYNTAX

Remove-ArrayObject [-Array] <Object> [-InputObject] <Object> [-WhatIf] [-Confirm] [<CommonParameters>]





DESCRIPTION

Removes all instances of the specified object in the specified array.

This is accomplished by using the .Remove() method in a While loop. (see NOTES)



By default, .Remove() only removes the first instance of a specified object.

If you want that behavior, call the method instead of using this function





PARAMETERS

-Array <Object>

A list, typically captured in a variable from New-ArrayList.

No validation is performed, but -Array assumes a collection of type

[System.Collections.ArrayList] or [System.Collections.Generic.List<T>].



Required? true

Position? 1

Default value

Accept pipeline input? false

Accept wildcard characters? false



-InputObject <Object>

One or more objects to remove from the provided array.

All matching objects will be removed.



Required? true

Position? 2

Default value

Accept pipeline input? true (ByValue)

Accept wildcard characters? false



-WhatIf [<SwitchParameter>]



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Confirm [<SwitchParameter>]



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



NOTES





https://www.sapien.com/blog/2014/11/18/ ... owershell/

https://p0w3rsh3ll.wordpress.com/2012/1 ... -an-array/



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>Remove-ArrayObject -Array $a -InputObject 13



Remove all instances of the number 13 from the collection in variable $a.









-------------------------- EXAMPLE 2 --------------------------



PS C:\\>'localhost' | Remove-ArrayObject $HostnameList



Remove all instances of the string 'localhost' from your $HostnameList array.

Pipeline input is supported, but optional.









-------------------------- EXAMPLE 3 --------------------------



PS C:\\>$srv = New-ArrayList



PS C:\\>Get-Service | Add-ArrayObject $srv



PS C:\\>$srv | Where-Object {$_.Name -like 'w*'} | Remove-ArrayObject $srv



Remove-ArrayObject will also remove complex matching objects.

Here, all services starting with "w" will be removed from the $srv array.











RELATED LINKS

https://github.com/brianbunke/ArrayList