Link to home
Start Free TrialLog in
Avatar of Straw C
Straw CFlag for Denmark

asked on

PowerShell : Get header columns count in a .CSV file

Powershell : How to get count of header columns  in a .CSV file ?


I have  different .CSV files with different header columns, i need a powershell script to get the count of header columns. 


for example : 


for Provider.csv


Provider_Id, Provider_Name,Provider_Location,Provider_ZIP


for Visit.csv:


Visit_Id,Visit_Location,Visit_Time


These are my 2 .csv files header column names for example.



Output: 

my desired output is : 

should have the same file name in the output,


Provider.csv 4 

Visit.csv 3


Avatar of Qlemo
Qlemo
Flag of Germany image

This is a simple approach:
get-childItem *.csv |
  % { [PSCustomObject] @{name = $_.Name; count = @((get-content $_ -TotalCount 1) -split ',').Count } }

Open in new window

Avatar of oBdA
oBdA

This uses the actual property count (so no issues if there's an embedded comma):
Get-ChildItem *.csv | Select-Object -Property FullName, Name, @{n='PropertyCount'; e={@((Import-Csv -LiteralPath $_.FullName | Select-Object -First 1).psobject.Properties).Count}}

Open in new window

Avatar of Straw C

ASKER

Hello, Thanks for the response. 


oBdA, I m trying to run this code for .dsv ('|' separated) file and i don't see any output when i m trying  to run it.

Here's the code i m using. I have given .cmd extension for the code using your code which you have given me for another solution.


 
@PowerShell.exe -Command "Invoke-Expression -Command ((Get-Content -Path '%~f0' | Select-Object -Skip 2) -join [environment]::NewLine)"
@exit /b %Errorlevel%


Get-ChildItem *.dsv | Select-Object -Property FullName, Name, @{n='PropertyCount'; e={@((Import-Csv -LiteralPath $_.FullName | Select-Object -First 1).psobject.Properties).Count}}
pause

Open in new window

Import-Csv uses by default a comma as delimiter; to use another one, you need to add the "Delimiter" parameter:
@PowerShell.exe -Command "Invoke-Expression -Command ((Get-Content -Path '%~f0' | Select-Object -Skip 2) -join [environment]::NewLine)"
@exit /b %Errorlevel%


Get-ChildItem *.dsv | Select-Object -Property FullName, Name, @{n='PropertyCount'; e={@((Import-Csv -LiteralPath $_.FullName -Delimiter '|' | Select-Object -First 1).psobject.Properties).Count}}
pause

Open in new window

Avatar of Straw C

ASKER

I tried to run the code and i don't see any output in cmd window ?
Avatar of Straw C

ASKER

I have 15 different .dsv files where im trying to run this code and i dont see any output in the cmd prompt window.
Avatar of Straw C

ASKER

Here's sample of how the files look like in the folder,

CMMDC_PMM_DWExtract_ALL_031522_04_30_40.dsv

CMMDC_PMM_DWExtract_ALL_031622_04_30_43.dsv



That's not an issue with the script itself, that's somehow connected to the disguise as a Batch file - you'll actually see the output after you press a key.
This should work:
@PowerShell.exe -Command "Invoke-Expression -Command ((Get-Content -Path '%~f0' | Select-Object -Skip 3) -join [environment]::NewLine)"
@pause
@exit /b %Errorlevel%


Get-ChildItem *.dsv | Select-Object -Property FullName, Name, @{n='PropertyCount'; e={@((Import-Csv -LiteralPath $_.FullName -Delimiter '|' | Select-Object -First 1).psobject.Properties).Count}}

Open in new window

Avatar of Straw C

ASKER

I m still getting an error when running the script,

Here's the error,


Invoke-Expression : At line:1 char:8
+ @exit /b %Errorlevel%
+        ~
You must provide a value expression following the '/' operator.
At line:1 char:8
+ @exit /b %Errorlevel%
+        ~
Unexpected token 'b' in expression or statement.
At line:1 char:1
+ @exit /b %Errorlevel%
+ ~~~~~
The splatting operator '@' cannot be used to reference variables in an expression. '@exit' can be used only as an
argument to a command. To reference variables in an expression use '$exit'.
At line:1 char:1
+ Invoke-Expression -Command ((Get-Content -Path '\\MMS\PDC\Informatica ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseException
    + FullyQualifiedErrorId : ExpectedValueExpression,Microsoft.PowerShell.Commands.InvokeExpressionCommand
You didn't use the script I posted, you just inserted the "@pause" line into your old script, but that was not the only change.
You missed the "... Select-Object -Skip 3 ..." in the first line, that's why you're getting these errors.
Avatar of Straw C

ASKER

I used the exact code which you posted,


@PowerShell.exe -Command "Invoke-Expression -Command ((Get-Content -Path '%~f0' | Select-Object -Skip 3) -join [environment]::NewLine)" 
@pause 
@exit /b %Errorlevel%  

Get-ChildItem *.dsv | Select-Object -Property FullName, Name, @{n='PropertyCount'; e={@((Import-Csv -LiteralPath $_.FullName -Delimiter '|' | Select-Object -First 1).psobject.Properties).Count}}

Open in new window

Avatar of Straw C

ASKER

I have edited the code now and still i don't see any output and there is no error this time. 
I am somewhat willing to bet that you didn't remove the "pause"in last line, which was the actual cause of the issue.
Avatar of Straw C

ASKER

i did remove "pause" in the last line. 😅
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Straw C

ASKER

Hi oBdA, its working fine when i give the path to get child item. Thank you so much.