Only exporting the the first object to CSV

$sg = Get-AzNetworkSecurityGroup -Name CCBDRSG01 | select -ExpandProperty NetworkInterfaces | select -Property id
$sg | Export-Csv -Path c:\temp\sg.csv
$text = Import-Csv -path c:\temp\sg.csv
$text | ForEach-Object {$“/subscriptions/1fc74016-76c6-45fd-ae38-adadadassdasd/resourceGroups/test/providers/Microsoft.Network/networkInterfaces/”) | Out-File c:\temp\sg2.csv}

it will only export out the first entry in the CSV file.

the reason I wanted to do this is Get-AzNetworkSecurityGroup does not support trim so i exported the data so I could use trim. If run the command with export it will display all the server interfaces.

get-help select-object -full

read through all of the parameters, and i think you’ll find your answer pretty quickly :slight_smile:

@David Schmidtberger Documentation is the -First place I would look too.

I always look a the get-help before I post and I must be dense (as I’m fairly new to powershell) I just do not see how the answer is select-object when it come to Export-CSV. I believe it has something to do with for-each not running through all objects. Can you give me another push as I have not found the answer.

You need to move that curly brace back in a number of characters so that your last line would look like

$text | ForEach-Object {$“/subscriptions/1fc74016-76c6-45fd-ae38-adadadassdasd/resourceGroups/test/providers/Microsoft.Network/networkInterfaces/”)} | Out-File c:\temp\sg2.csv

The way you had it enclosed the ‘Out-File c:\temp\sg2.csv’ code portion inside the ForEach loop, resulting in each iteration over-writing the one before, and sg2.csv ends up with the last value only.
Also consider calling it sg2.txt instead of sg2.csv


Oops, thought you were asking how to only return the first item, not that you are only returning the first item. First, using trim like that is very specific and if anything in the path changed before what you want to get, then it would fail. Here is another method to get the end of a delimited string:

#current method

#better method
(“/subscriptions/1fc74016-76c6-45fd-ae38-adadadassdasd/resourceGroups/WhatIWant" -split '/')[-1]

As far as the problem at hand, there is no need to export, import and for loop in this scenario. You can use a calculated expression to get the value:

$sg = Get-AzNetworkSecurityGroup -Name CCBDRSG01 | 
      select -ExpandProperty NetworkInterfaces | 
      select -Property @{Name='id';Expression={($ -split '/')[-1]}} 

This work but I want to make sure I understand the part of the command

@{Name=‘id’;Expression={($ -split ‘/’)[-1]}}

(@ = at) than we are looking for id Property named Name Expression this I’m unsure of however I believe it used to pass the pipeline property id and than split after / [-1] tells it to look at the last / that it finds. Is this correct?

Its the calculated property in PowerShell where we can create custom properties just by calculating it from existing property.
E -> is the expression, literally any code can be put inside the scriptblock, her $_, currentelement in the pipeline and doing the split operation here for constructing the output

Name -> is the name of the property being calculated, this can be any string.

more insight here: Using PowerShell's Calculated Properties -- Microsoft Certified Professional Magazine Online