0

I'd like to store the output of the command Select in a variable. Here's the original code:

# OUs to search for servers
"OU=Domain Controllers,$mydomain", "OU=Server,OU=Berlin,$mydomain" | 
# For each OU get Windows Server
ForEach { Get-ADComputer -Filter { OperatingSystem -Like '*Windows Server*' } -Properties OperatingSystem -SearchBase $_ } | 
Select -Exp Name | Add-Content C:\serverfile.txt

In the last line I'd like to change Add-Content to a command that adds the output to a variable $Servers. However I can't get the syntax right. I tried:

| Add-Content $Servers

| $Servers

"> $Servers"

$Servers += Select -Exp Name

2 Answers 2

1
  • Add-Content $Servers - Add-content would expect that $servers is a file path. This is not what you want. See more from Add-Content.
  • | $Servers - $servers is not a function, cmdlet, alias or command that accepts pipeline input. This is not what you want. Here is a video that can be a start for understanding the pipeline. Not the best example but it is a start.
  • "> $Servers" - Redirection is also supposed to be used for file paths (for the most part.). $servers is not a file path. This is not what you want. See more from about_redirection.

You can just assign the output from the pipeline to a variable directly. This is what you could do. Other options do exist as well like building the array inside the loop. That is unnecessary.

$servers = "OU=Domain Controllers,$mydomain", "OU=Server,OU=Berlin,$mydomain" | ForEach-Object { 
    Get-ADComputer -Filter { OperatingSystem -Like '*Windows Server*' } -Properties OperatingSystem -SearchBase $_ } | 
    Select-Object -ExpandProperty Name

Or if you need to do both output to file and variable you can use Tee-Object

"OU=Domain Controllers,$mydomain", "OU=Server,OU=Berlin,$mydomain" | ForEach-Object { 
    Get-ADComputer -Filter { OperatingSystem -Like '*Windows Server*' } -Properties OperatingSystem -SearchBase $_ } | 
    Select-Object -ExpandProperty Name | Tee-Object -Variable servers | Add-Content "C:\serverfile.txt"

In both cases $servers will contain the output.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the explanations. I always looked for a way to put the variable after the pipeline, but I know now that this was the wrong approach. I used the solution of @gmsoulman, it seemed to fit my script best.
@Peter If you start doing 1000's of computer be careful as that is a memory intensive operation. Building an array like that make PowerShell build a new array and then append the new object. Glad you found a solution.
Thanks for your advice. I will be doing it for only about 100 servers. And I eventually used the other solution.
0

You can create and array ($Servers for example) then add each result into it:

$Servers = @()
"OU=Domain Controllers,$mydomain", "OU=Server,OU=Berlin,$mydomain" | 
# For each OU get Windows Server
ForEach { $Servers += Get-ADComputer -Filter { OperatingSystem -Like '*Windows Server*' } -Properties OperatingSystem -SearchBase $_ | Select -ExpandProperty Name} 

1 Comment

Also, you can replace $Servers += with [array]$Servers += and avoid having to initialise the array (i.e. no need for $Servers = @().)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.