Export Foreach-Object Powershell

Ragz 1 Reputation point
2020-11-23T16:13:14.673+00:00

Hi ,

Need to export the below code to csv

Get-MsolAccountSku | ForEach-Object {
Write-Host "n"$_.AccountSkuId Write-Host "t* SKUId:"$.SkuId
$result = $
.ServiceStatus | ForEach-Object {
Write-Host "tt+" $.ServicePlan.ServiceName
Write-Host "tt`t- ServiceType:" $
.ServicePlan.ServiceType
select $_.ServicePlan.ServiceName
}
}

$result | export-CSV 'e:\temp\Chart.csv'

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Bill Stewart 181 Reputation points
    2020-11-24T15:27:56.687+00:00

    Keep in mind that Write-Host does not produce output to the pipeline (it goes to the host only, so there's nothing to export).

    1 person found this answer helpful.
    0 comments No comments

  2. Rana Banerjee 1 Reputation point
    2020-11-23T23:12:24.033+00:00

    What is your main objective, to try and get licence information for each user?

    0 comments No comments

  3. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,971 Reputation points Microsoft Vendor
    2020-11-24T07:07:50.17+00:00

    Hi,

    There's no object piped to select so the output is null. And the inner for-each loop is unnecessary as each object has only one ServiceStatus property.

    $result = Get-MsolAccountSku | ForEach-Object {  
        Write-Host "`n"$_.AccountSkuId   
        Write-Host "`t* SKUId:"$_.SkuId  
        Write-Host "`t`t+" $_.ServiceStatus.ServicePlan.ServiceName  
        Write-Host "`t`t`t- ServiceType:" $_.ServiceStatus.ServicePlan.ServiceType  
        $_.ServiceStatus.ServicePlan.ServiceName  
    }   
    $result | export-CSV 'e:\temp\Chart.csv'  
    

    Best Regards,
    Ian

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

    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