Reputation: 14962
I have a script where i define the following variable for a build script:
$globalExcludePath = @('obj', 'App_Data', 'Properties' )
I'd like to remove an item in a specific function but the remove function doesn't exist and i couldn't find any pointer on how to do it.
$customExcludePath = $globalExcludePath
$customExcludePath.Remove('App_Data') # fails
Any idea on how to do it?
Upvotes: 0
Views: 86
Reputation: 68273
Arrays don't have a Remove method. You can put it into a generic collection that does.
One option is to explicitly load it into a specific collection type, like arralylist
$globalExcludePath = [collections.arraylist]('obj', 'App_Data', 'Properties' )
$globalExcludePath.Remove('App_Data')
$globalExcludePath
Another option is to use the scriptblock .invoke() method, which returns a collection of generic powershell objects:
$globalExcludePath = {'obj', 'App_Data', 'Properties'}.Invoke()
$globalExcludePath.Remove('App_Data') > $nul
$globalExcludePath
The remove method on that type returns True or False depending on whether the remove operation was successful, and you probably want to redirect that to $nul so it doesn't pollute the pipeline.
Upvotes: 1
Reputation: 60918
Simple Array are size fixed. You can do this:
$customExcludePath = $globalExcludePath | ? { $_ -ne 'App_Data' }
or use [list]
[System.Collections.Generic.List[string]]$customExcludePath = $globalExcludePath
$customExcludePath.Remove('App_Data')
True
$customExcludePath
obj
Properties
Upvotes: 1