How to get only values for each server rather than the property names for every server

Anudeep satrasala 0 Reputation points
2023-01-31T19:30:03.2733333+00:00
$servers = Get-Content -path C:\temp\fileshares.txt

foreach ($server in $servers) {
    Write-Host "Getting file shares from $server..."
    $fileshare = Get-WmiObject -Class Win32_Share -ComputerName $server | 
    Where-Object {$_.Name -like "*$"} | 
    Select-Object @{n="Server";e={$server}}, Name, Path, @{n="Owner";e={(Get-Acl $_.Path).Owner}}
    $fileshare | out-file C:\temp\result.csv -Append
} 

output is showing in single column in excel and also column names is showing for every server. please help to get column names only once and data for all servers



PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,583 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Ian Xue 37,621 Reputation points Microsoft Vendor
    2023-02-01T01:59:13.57+00:00

    Hi,

    You can store all the results in one array.

    $servers = Get-Content -path C:\temp\fileshares.txt
    
    $fileshare = @()
    foreach ($server in $servers) {
        Write-Host "Getting file shares from $server..."
        $fileshare += Get-WmiObject -Class Win32_Share -ComputerName $server | 
        Where-Object {$_.Name -like "*$"} | 
        Select-Object @{n="Server";e={$server}}, Name, Path, @{n="Owner";e={(Get-Acl $_.Path).Owner}}
    
    } 
    $fileshare | out-file C:\temp\result.csv 
    

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Limitless Technology 44,386 Reputation points
    2023-02-02T10:33:25.7766667+00:00

    Double post

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.