0

existing code:

$in = Import-Csv .\in.csv
$TextInfo = (New-Object System.Globalization.CultureInfo("en-US")).TextInfo;
$in | ForEach-Object { $_.name = $textInfo.ToUpper($_.name) }
$in | Export-Csv .\out.csv

This will convert the case ToUpper or can be ToTitleCase optionally.

How would each column be iterated?

Tried to get the column names as below. Just need to better understand how to iterate each column rather than a specific named column. How is the count of CSV columns determined? How is each column referenced in a loop as above?

Ideally, using the REPL console as below.

PS C:\Users\Nick\Desktop>
PS C:\Users\Nick\Desktop> $in = Import-Csv .\in.csv
PS C:\Users\Nick\Desktop>
PS C:\Users\Nick\Desktop> $TextInfo = (New-Object System.Globalization.CultureInfo("en-US")).TextInfo;
PS C:\Users\Nick\Desktop> $in.PSObject.Properties.Name
Count
Length
LongLength
Rank
SyncRoot
IsReadOnly
IsFixedSize
IsSynchronized
PS C:\Users\Nick\Desktop>
PS C:\Users\Nick\Desktop> $in | Format-Table

alpha      beta       charlie    delta
-----      ----       -------    -----
sdkljgsdf  bdgfhgfnhm ngfgfddfsg dfhgsdfg
gfjdsaklbf dfbdfgfn   vfsdafdfbn fgjtyj
bdgfkl     dsfsd      vgngfas    dsfhgtj
dsbmnkl    nggfn      bfdsda     edtshdgf
asdfkl     sdagfdfh   vbdfdngt   ngfggj


PS C:\Users\Nick\Desktop>
PS C:\Users\Nick\Desktop> $in.PSObject.Properties[2].Value
PS C:\Users\Nick\Desktop>

Also:

PS C:\Users\Nick\Desktop>
PS C:\Users\Nick\Desktop>
PS C:\Users\Nick\Desktop>
PS C:\Users\Nick\Desktop> $in | Get-Member -MemberType Properties


   TypeName: System.Management.Automation.PSCustomObject

Name    MemberType   Definition
----    ----------   ----------
alpha   NoteProperty string alpha=sdkljgsdf
beta    NoteProperty string beta=bdgfhgfnhm
charlie NoteProperty string charlie=ngfgfddfsg
delta   NoteProperty string delta=dfhgsdfg


PS C:\Users\Nick\Desktop>

will get the column names: alpha, beta, etc. But how would each be passed and iterated to change the case of all "cells" as above?

see also:

PowerShell - Import-Csv - referencing a column by its position

How to convert a specific CSV column to TitleCase with PowerShell from the REPL console?

https://stackoverflow.com/a/34131869/22063140

  • I want to iterate each column. Not a specific column @SantiagoSquarzon and I can kinda get each column name as above. – Nicholas Saunders Jun 21 '23 at 21:43
  • 1
    His last example from that answer explicitly says: `# Iterate over all properties and modify their values.` then uses `foreach ($prop in $_.psobject.Properties) {`. This is how you "iterate over each column from each row" – Santiago Squarzon Jun 21 '23 at 21:45
  • Maybe the question is asked poorly. I'll try again. – Nicholas Saunders Jun 21 '23 at 22:09
  • 1
    Did you try the code in the latest example of that answer? There are 2 loops, an outer `ForEach-Object` which loops thru each row then there is an inner `foreach` which loops thru each property (column). If that didn't do what you were expecting then edit your question and I'll reopen it – Santiago Squarzon Jun 21 '23 at 22:12
  • 1
    yes @SantiagoSquarzon I was just taking it to be a diff question. Thx for all your efforts. Above and beyond. – Nicholas Saunders Jun 21 '23 at 22:44

0 Answers0