Introduction
There is one way to add AD Computers to a PowerShell array. A PowerShell array is a data structure that stores a collection of items.
Ideally, when you run PowerShell Get-ADComputer command the result should automatically be stored in an array. But some admins may run into problem working with the data because of the way the array is stored.
In this guide I will simplify how to add AD Computers to a PowerShell array. I will show different ways to manipulate the data in the array. The guide also covers how to use the AD Computers in a PowerShell array in a ForEach statement.
Before you start manipulating AD Computers in a Powershell array, you have to retrieve the computers. The next section will demonstrate how to get AD computers using PowerShell Get-ADComputer command.
How to Get AD Computers from Active Directory
To get AD computers, run the command below:
Get-ADComputer -Filter * -Properties Name | Select-Object Name
The command returns the names of all computers in the domain.
Refer to the result of the command above. I said earlier that when you retrieve AD computers it is already in a PowerShell array.
To confirm this run the command below:
(Get-ADComputer -Filter * -Properties Name | Select-Object Name).GetType()
Here is the result
I simply wrapped the previous command in an () operator and used the GetType method to determine the type of PowerShell object returned.
The result confirms that the result of Get-ADComputer command is a System.array.
So, if it is an array why do people still search “powershell add ad computers to array”?
In the remaining part of this guide I will answer this question. Including how to add AD computers to an array via a variable. And how to prepare the data for use in a ForEach statement.
PowerShell Add AD Computers to an Array
Here are various ways you can add AD computers to an array:
PowerShell Add AD Computers to an Array Without a Variable
As already discussed in the last section, when you retrieve computers from Active Directory using Get-ADComputer, the result is already in an array.
I believe the reason most people may think that it is not in an array is because of the way the data is presented.
Lets revisit the the command. Here it is:
Get-ADComputer -Filter * -Properties Name | Select-Object Name
And the result:
Notice that the result has a header, called Name. This is the object property we returned with the Select-Object command.
On its own, this is not a problem. But if you try to use this in a ForEach statement it will return errors. Especially if you try to use the value in another PowerShell command within the ForEach statement.
How do you return the values without the property header? There are two ways you can do this.
One, by amending the command as shown below:
(Get-ADComputer -Filter * -Properties Name | Select-Object Name).Name
Notice that the header is gone!
Another way to remove the headers from the result is to use the -ExpandProperty parameter of Select-Object.
Get-ADComputer -Filter * -Properties Name | Select-Object -ExpandProperty Name
Here is the result with the header done.
PowerShell Add AD Computers to an Array With a Variable
In PowerShell scripting, variables make it easy to manage data. In the first instance, it reduces the length of your scripts.
To add AD Computers to a PowerShell array using a variable, modify your command as shown below:
$ADComputers = Get-ADComputer -Filter * -Properties Name | Select-Object -ExpandProperty Name
The above command will appear not to return a result. Why? Because the result is now stored in the $ADComputers variable!
To confirm that the result is stored in $ADComputers variable, run the command below
$ADComputers
Here is your result
But are the AD computers in a PowerShell array? Let’s find out with the command below:
$ADComputers.GetType()
The result shows that the AD Computers are still in an array even though they are stored in a variable!
PowerShell Add AD Computers to an Array and ForEach Statement
A PowerShell ForEach statement allows you to iterate through a collection of objects to perform specific tasks.
One application could be that you want to update the property of each AD computer you have retrieved with the Get-ADComputer command.
For this command, I am assuming that AD computers are stored in the $ADComputers variable. Here is how you combine the result of AD computers in your array with a ForEach statement.
ForEach ($ADComputer in $ADComputers) {
<run a command on each $ADComputer>
}
Conclusion
Adding AD computers to a PowerShell array is as simple as retrieving the computers using the Get-ADComputer command. This guide covered this but also demonstrated how to manipulate the result to remove headers. It also covered how to add the results to a variable and use ForEach to loop through the result.
I hope the guide was useful. If you have any questions or comments please post them on [discourse_topic_url]. Alternatively, you could share your experience manipulating AD computers in a PowerShell array.
To learn more about PowerShell visit our PowerShell page. Better still, for a list of all our PowerShell guides, search PowerShell (search link opens in a new browser tab).
Other Helpful Guides
- PowerShell Script to Get Active Directory Group Members
- Powershell Try Catch Finally and Error Handling
Additional Resources and References
- Get-ADComputer
- About ForEach
- [discourse_topic_url]