PowerShell Performance: += Operator versus ArrayList to add Elements to an Array

Sometimes you need to add elements to an array in PowerShell within a loop without knowing beforehand how many elements you are going to add.

In the most cases using the += operator make it all work easily.
Since the += operator actually…

  • …creates a new array in the memory with a length of the current array +1
  • copies all the elements of the current array into the new array
  • adds the new element at the end of the new array
  • deletes the current array from memory…

…the whole process is extremely time consuming (or expensive) if your array grows larger and larger.
On the other hand, using the .Add() function at a System.Collections.ArrayList just adds a new element at the end of the array without copying it first, which is much faster.

Therefore, I would suggest using the .NET System.Collections.ArrayList instead as shown below.

To demonstrate the massive difference in performance between the += operator and the System.Collections.ArrayList, I wrote two short scripts to demonstrates the effect.
Read More

How to change your Password in a Remote Session (Windows Security)

Using a jump server to connect from your computer to different servers in your network makes it quite uncomfortable to change your password in a Windows Server 2012 environment.

Pressing Ctrl + Alt + End (instead of Delete) opens Windows Security on the jump server and not on the server you are remoting on using the jump server.

There is an easy solution, though: create a new .vbs file on the remote computer i.e. “WindowsSecurity.vbs” and add the following code:

1
2
Set objShell = CreateObject("Shell.Application")
objShell.WindowsSecurity

Now all you have to do is just double click the file and Windows Security pops up which allows you to change your password.