Sorting output of Get-ADComputer efficiently

2

Powershell beginner here. I want to run a command in the terminal to list all the domain members of the specified domain with select properties, sorted by name. I have a domain with just under 400 PCs. I pieced together some stuff from online guides. The command I wrote is:

$props = @('Name', 'OperatingSystem', 'OperatingSystemVersion', 'IPv4Address'); Get-ADComputer -Filter * -Property $props -SearchBase "DC=domainname,DC=com" -Server "domainname.com" -Credential "domainname.com\domainuser" | Sort-Object -Property Name | Format-Table $props -Wrap –Auto

Sorting by name is a convenience; I don't absolutely need it but it would be nice. The above query gets the expected results but it takes quite a long time. With the sorting step it takes about 20 seconds to output the results. Without the sorting it takes about 1 second.

Is there any more efficient way to sort the output?

Nikolas_X

Posted 2018-09-27T05:53:12.647

Reputation: 51

Answers

3

My bad, I didn't look deep enough into it when asking the question. For future reference, the solution is to add a Select step like so:

$props = @('Name', 'OperatingSystem', 'OperatingSystemVersion', 'IPv4Address'); Get-ADComputer -Filter * -Property $props -SearchBase "DC=domainname,DC=com" -Server "domainname.com" -Credential "domainname.com\domainuser" | Select $props | Sort-Object -Property Name | Format-Table $props -Wrap –Auto

Nikolas_X

Posted 2018-09-27T05:53:12.647

Reputation: 51