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).
Export Foreach-Object Powershell
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 "t
t+" $.ServicePlan.ServiceName
Write-Host "t
t`t- ServiceType:" $.ServicePlan.ServiceType
select $_.ServicePlan.ServiceName
}
}
$result | export-CSV 'e:\temp\Chart.csv'
3 answers
Sort by: Most helpful
-
Bill Stewart 181 Reputation points
2020-11-24T15:27:56.687+00:00 -
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?
-
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.