Powershell: add an item to a collection

Is it possible to add an item to a collection in PowerShell (I'm not sure if I'm using the correct terms here).  Let me give an example:

I want to get a list of all the servers in our environment, and then add another server name to the list along with a tag on whether it's enabled or not.

So I run this command:
$AllServers = Get-ADComputer -Filter * -Properties "OperatingSystem" | Where-Object {$_.OperatingSystem -like "*Server*"} | Select Name, Enabled | Sort-Object Name

Then I want to add a server with a Name of "Server A" and an Enabled value of $false to the $AllServers variable.

How do I accomplish this?

Thanks!  Greg.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can always add items to a Powershell array using the += operator:
$a = @()
$a += 'a'
$a += 'b'

Open in new window

So you could use something like the following.
The @() around the first command will make sure that $AllServers will be an array, even if only one element is returned by Get-ADComputer.
The last line generates a custom object with the two properties and adds it to the array.
$AllServers = @(
	Get-ADComputer -Filter * -Properties "OperatingSystem" |
		Where-Object {$_.OperatingSystem -like "*Server*"} |
		Select-Object -Property Name, Enabled |
		Sort-Object Name
$AllServers += '' | Select-Object -Property @{n='Name'; e={'ServerA'}}, @{n='Enabled'; e={$false}}

Open in new window

Or if you want the added server not at the end of the array, but at its sorted position:
$AllServers = $(
	Get-ADComputer -Filter * -Properties "OperatingSystem" |
		Where-Object {$_.OperatingSystem -like "*Server*"} |
		Select-Object -Property Name, Enabled
	'' | Select-Object -Property @{n='Name'; e={'ServerA'}}, @{n='Enabled'; e={$false}}
) | Sort-Object Name

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
malcolm29Author Commented:
Thank you!  This worked GREAT!  And your thoughtfulness in providing two options allowed me to much better understand how PowerShell does this.  Thank you!  Greg.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.