Revised efforts based on supplied answers:
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> Get-Content ./case.csv | ForEach-Object ToUpper
FJKDLA,W
FKDSLAJF,FDJK;A
NLK;NBF;SDJF,DGDF
VNL;KKDF,BGNGFN
NVCL;V,RGS
NVKL;,THRN
VLKDF,TMMJYMF
FJDK,FDJK;A
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> Get-Content ./case.csv | ForEach-Object ToLower
fjkdla,w
fkdslajf,fdjk;a
nlk;nbf;sdjf,dgdf
vnl;kkdf,bgngfn
nvcl;v,rgs
nvkl;,thrn
vlkdf,tmmjymf
fjdk,fdjk;a
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $TextInfo = (New-Object System.Globalization.CultureInfo("en-US")).TextInfo;
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> Get-Content ./case.csv | ForEach-Object ToTiteCase
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> pwsh --version
PowerShell 7.3.4
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 23.04
Release: 23.04
Codename: lunar
PS /home/nicholas/powershell>
My main concern is more converting to TitleCase, and, ideally, from the REPL console rather than a script file. I haven’t been able update efforts on targeting a specific column.
By the REPL console I mean the interactive shell, if that makes sense.
All answers and comments have been extremely helpful and appreciated.
additional info:
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> Get-Culture
LCID Name DisplayName
---- ---- -----------
1033 en-US English (United States)
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> (Get-Culture).TextInfo.ToTitleCase($_.fjkdla)
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> (Get-Culture).TextInfo.ToTitleCase("hmm")
Hmm
PS /home/nicholas/powershell>
Which looks to be the desired output for a single string. Not sure how to iterate the CSV file above from the console, however.
2
Answers
Update in response to the now substantially changed question:
Your original question, as still reflected in the title, was about CSV data, and targeting a specific column. Given that you originally used
Import-Csv
, an OO approach was called for, as shown in the next section.Import-Csv
‘s-Header
parameter, e.g.:Import-Csv ./case.csv -Header Name, Value
.ForEach-Object
script block, as shown below (e.g.,$_.Name
)Your updated question, which uses plain-text processing of your CSV file using
Get-Content
, seemingly attempts to convert all column values to title case; to that end, use the following approach:Answer to the original, OO question:
To convert a single, known column to title case in a single pipeline, be sure to use its exact name (though case doesn’t matter); using your sample CSV’s first column name as an example:
This outputs the modified objects parsed from the CSV file and outputs them directly; by default, they print to the screen; prepend, e.g.,
$csv =
to the pipeline to capture its output in variable$csv
.Note:
If you’ve already imported your CSV file and saved the resulting objects to array
$csv
, iRon’s suggestion is a simple and efficient solution, using the intrinsic.ForEach()
method:To methodically convert all columns to title case, without needing to know the column (property) values ahead of time, using the intrinsic
psobject
property:Note:
Get-Culture
to obtain the title-casing method not only for each object, but, in the last case, also for each property of each object, which is inefficient.Better-performing alternative, which caches a reference to the method and invokes it with
.Invoke()
:The Get-Culture PowerShell cmdlet doesn’t seem to exist on the Replit PowerShell console. It has a TextInfo method you can use to get Title Case, as @mklement shows. The below code worked for me in Replit console, however.
CSV File:
REPLIT Console Code:
Output: