- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add/remove values in the array in PowerShell?
An array is always a fixed size. To add value to the array, you need to create a new copy of the array and add value to it. To do so, you simply need to use += operator.
For example, you have an existing array as given below.
$array = 1,2,3,4,5
To add value “Hello” to the array, we will use += sign.
$array += "Hello"
Now, we will check the output of the array.
We have another method to add value to the array. By Add() operation of the array.
$array.Add("Hi")
When you use the above method to add a variable in the given array, you will get below error.
Exception calling "Add" with "1" argument(s): "Collection was of a fixed size." At line:1 char:1 + $array.Add("Hi") + ~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : NotSupportedException
This is because the array size is fixed. You can check if the array is a fixed size or not by using the below method.
$array.IsfixedSize
When you check the type of this array, it is an object, not the list.
To deal with the above problem, we need to use System.Collection.ArrayList instead.
When you check the type of this array, it will be an array list.
So from the array list, we can add or remove items.
$array.Add("Hello")
$array.Remove("Delta")
- Related Articles
- How to add and remove values to the Hash Table in PowerShell?
- How to add multiple values in the Hashtable using PowerShell?
- How to remove duplicate property values in array – JavaScript?
- How to remove false values from an array in JavaScript?
- How to remove falsy values from an array in JavaScript?
- How to add help in the PowerShell function?
- How to create an array in PowerShell?
- How to use Array Splatting in PowerShell?
- How to add/merge two hash tables in PowerShell?
- How to add a new element in the XML using PowerShell?
- How to add the entry in the windows host file using PowerShell?
- How to add values to the dictionary in Python?
- How to overwrite or remove PowerShell alias?
- How to remove windows features using PowerShell?
- How to remove Windows service with PowerShell?
