How to display the value of variable "$computers" after SELECT

Sean H 0 Reputation points
2023-03-16T02:07:44.09+00:00

In the following script how can I display the value of $computers variable before it writes the entry to the file and grabs the next value.

I want to display the value of $computers before it is passed on to the Get=WmiObject.

======================================================================

$computers = Get-Content -Path C:\Get-LocalUser.csv

Get-WmiObject -ComputerName $computers -Class Win32_UserAccount -Filter "LocalAccount='True'" |

Select PSComputername, Name | Export-csv C:\local_users2.csv -NoTypeInformation

=======================================================================

Thank You,

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,592 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,832 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,039 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Limitless Technology 43,926 Reputation points
    2023-03-16T15:11:36.9333333+00:00

    Hello

    Thank you for your question and reaching out. I can understand you are having query\issues related to Variable in PowerShell.

    You can use in your script to display the value of variable "$computers"

    Write-Output $computers

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

  2. Limitless Technology 43,926 Reputation points
    2023-03-16T15:11:46.8133333+00:00

    Hello

    Thank you for your question and reaching out. I can understand you are having query\issues related to Variable in PowerShell.

    You can use in your script to display the value of variable "$computers"

    Write-Output $computers

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

  3. Rich Matheisen 44,776 Reputation points
    2023-03-16T15:27:35.0533333+00:00

    Just add a new line with just the variable name $computers between the Get-Content and the Get-WMIObject cmdlets.

    That will send the contents of the $computers array (or scalar, depending on the number of names retrieved from the file) to the success stream. Because you're not piping the resulting data into the Get-WMIObject there's no problem with sending that data into success stream.


  4. Rich Matheisen 44,776 Reputation points
    2023-03-16T15:38:15.4833333+00:00

    An alternative way of sending the data to the console while also placing it into a variable is to use the Tee-Object cmdlet:

    Get-Content -Path C:\Get-LocalUser.csv | Tee-Object computers    # NOTE: there is NO $ before the variable name!
    Get-WmiObject -ComputerName $computers -Class Win32_UserAccount -Filter "LocalAccount='True'" |
        Select PSComputername, Name | Export-csv C:\local_users2.csv -NoTypeInformation
    
    0 comments No comments

  5. Rich Matheisen 44,776 Reputation points
    2023-03-17T18:43:40.2433333+00:00

    Is there a reason you chose to use Get-WMIObject instead of using Invoke-Command and the Get-LocalUser?

    $computers = Get-Content -Path C:\Get-LocalUser.csv
    Invoke-Command -ComputerName $computers -ScriptBlock {Get-LocalUser} 2>&1 |
        ForEach-Object{
            if ($_ -is [System.Management.Automation.ErrorRecord]){
                $_ | Select-Object @{n="PSComputername";e={$_.CategoryInfo.TargetName}}, Name, @{n="Error";e={$_.Exception}}
            }
            else{
                $_ | Select-Object PSComputername, Name, Error
            }
        }
    
    0 comments No comments