- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
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.
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.
- How to add and remove values to the Hash Table in PowerShell?
- How to add multiple values in the Hashtable using PowerShell?
- How to add help in the PowerShell function?
- How to remove the mapped network drive using PowerShell?
- How to add the entry in the windows host file using PowerShell?
- How to add a new element in the XML using PowerShell?
- How to overwrite or remove PowerShell alias?
- How to remove windows features using PowerShell?
- How to remove Windows service with PowerShell?
- How to add/merge two hash tables in PowerShell?
- How to Remove the computer from the AD domain using PowerShell?
- How to remove pagefile on the specific drive using PowerShell?
- How to add the tag of Azure VM using PowerShell?