# How to ignore hidden excel columns and export only visible ones to csv?

**URL:** <https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291>\
**Category:** PowerShell Help\
**Created:** [April 29, 2020, 4:29pm UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291 "2020-04-29T16:29:18Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![cataster](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@cataster](https://forums.powershell.org/u/cataster)\
**Post date:** [April 29, 2020, 4:29pm UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/1 "2020-04-29T16:29:18Z")

</div>

Ok, so there are threads like [this](https://stackoverflow.com/questions/39897199/export-only-visible-rows-to-csv-with-powershell) to export only visible rows to csv, but how about columns? do i need a loop? or can it be done as easily as visible worksheets?

```auto
Begin {
$excel = New-Object -ComObject Excel.Application -Property @{
Visible = $false
DisplayAlerts = $false
}
}
Process {
#$root = Split-Path -Path $Path
$filename = [System.IO.Path]::GetFileNameWithoutExtension($Path)
$workbook = $excel.Workbooks.Open($Path)

foreach ($worksheet in ($workbook.Worksheets | Where { <# $_.Visible -eq -1 #> $_.Name -ne 'Security' -and $_.Name -ne 'Notes' })) {
<# WIP if($ExcludeHiddenColumns) { if ($worksheet.Column.Visible -eq -1) { $worksheet.sheets.columns.entirecolumn.hidden=$true } } #>

if ($ExcludeHiddenSheets) {
if ($worksheet.Visible -eq -1) {
$ws = $worksheet
}
}
else {
$ws = $worksheet
}

if ($AppendFileName) {
$name = Join-Path -Path $csvPATH <# $root #> -ChildPath "${filename}_$($ws.Name).csv"
}
else {
$name = Join-Path -Path $csvPATH <# $root #> -ChildPath "$($ws.Name).csv"
}

try {
$ws.SaveAs($name, 6) #6 to ignore formatting and convert to pure text, otherwise, file could end up containing rubbish
}
catch {
Write-Error -Message "Failed to save csv! Path: '$name'. $PSItem"
}
}
}

```

Particularly, this is the part im working on to tell the script to export only the visible columns if specified by the boolean param `$ExcludeHiddenColumns`, but not sure the best way to approach this…

```auto
 <# WIP
if($ExcludeHiddenColumns) {
if ($worksheet.Column.Visible -eq -1) {
$worksheet.sheets.columns.entirecolumn.hidden=$true
}
}

```

[related](https://stackoverflow.com/questions/61511673/how-to-export-only-visible-excel-columns-to-csv)

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [April 29, 2020, 7:26pm UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/2 "2020-04-29T19:26:23Z")

</div>

I have to ask, why work on the CSV through Excel com object instead of directly with it in powershell? It just complicates it so if it’s not a requirment, I recommend working natively with CSV.

---

<div class="post-metadata">

**Author:** ![cataster](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@cataster](https://forums.powershell.org/u/cataster)\
**Post date:** [April 29, 2020, 7:29pm UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/3 "2020-04-29T19:29:42Z")

</div>

[quote quote=223923]I have to ask, why work on the CSV through outlook com object instead of directly with it in powershell? It just complicates it so if it’s not a requirment, I recommend working natively with CSV.

[/quote]  
Good question. Its because the input is an excel file, so my task now is to convert it to csv, given the above code, it works great! I just wish it doesnt include the hidden columns

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [April 29, 2020, 7:35pm UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/4 "2020-04-29T19:35:39Z")

</div>

Well you could save the excel file to csv and then easily filter the columns out that shouldn’t be there. But, I’m sure there is a way to do what you are wanting. If someone hasn’t answered when I get some time, I will see what I can find!

---

<div class="post-metadata">

**Author:** ![cataster](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@cataster](https://forums.powershell.org/u/cataster)\
**Post date:** [April 29, 2020, 7:47pm UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/5 "2020-04-29T19:47:20Z")

</div>

[quote quote=223932]Well you could save the excel file to csv and then easily filter the columns out that shouldn’t be there. But, I’m sure there is a way to do what you are wanting. If someone hasn’t answered when I get some time, I will see what I can find!

[/quote]  
Sounds good, thanks!

---

<div class="post-metadata">

**Author:** ![cataster](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@cataster](https://forums.powershell.org/u/cataster)\
**Post date:** [April 30, 2020, 12:06am UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/6 "2020-04-30T00:06:05Z")

</div>

[quote quote=223932]Well you could save the excel file to csv and then easily filter the columns out that shouldn’t be there. But, I’m sure there is a way to do what you are wanting. If someone hasn’t answered when I get some time, I will see what I can find!

[/quote]

After a lot of trial and error, i figured it out!

```auto
foreach ($worksheet in ($workbook.Worksheets | Where { <# $_.Visible -eq -1 #> $_.Name -ne 'Security' -and $_.Name -ne 'Notes' })) {        
    if($ExcludeHiddenColumns) {
        $ColumnsCount = $worksheet.UsedRange.Columns.Count
        for ($i=1; $i -le $ColumnsCount; $i++)
        {
            $column = $worksheet.Columns.Item($i).EntireColumn #$worksheet.sheets.columns.entirecolumn.hidden=$true
            if ($column.hidden -eq $true)
            {
                $column.Delete()
                #$i = $i - 1
            }
        }
    }
}

```

&nbsp;

---

<div class="post-metadata">

**Author:** ![cataster](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@cataster](https://forums.powershell.org/u/cataster)\
**Post date:** [April 30, 2020, 12:46am UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/7 "2020-04-30T00:46:51Z")

</div>

> [@](#):
>
> > [Doug Maurer wrote:](https://powershell.org/forums/topic/how-to-ignore-hidden-columns-and-export-only-visible-ones-to-csv/#post-223932)
> > Well you could save the excel file to csv and then easily filter the columns out that shouldn’t be there. But, I’m sure there is a way to do what you are wanting. If someone hasn’t answered when I get some time, I will see what I can find!
> 
> Sounds good, thanks!

Now that i resolved that issue, the question is: how do i print the column name being deleted? i tried the following but its not printing out anything

**$($worksheet.Columns.Item($i).EntireColumn.Name)**

or **$column.Name**

or **$worksheet.Columns.Item($i).Name**

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [April 30, 2020, 12:56am UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/8 "2020-04-30T00:56:17Z")

</div>

I got the list of hidden column headers like this.

```
$hiddencolumnheaders = foreach($column in $worksheet.UsedRange.Columns){
    if($column.hidden -eq $true){
        $column.value2 | select -first 1
    }
}
```

---

<div class="post-metadata">

**Author:** ![cataster](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@cataster](https://forums.powershell.org/u/cataster)\
**Post date:** [April 30, 2020, 1:00am UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/9 "2020-04-30T01:00:45Z")

</div>

\<p style=“padding-left: 40px;”\>[quote quote=224040]I got the list of hidden column headers like this.

\</p\>

PowerShell

6 lines

\<textarea class="ace\_text-input" style="opacity: 0; height: 17.9048px; width: 7.2381px; left: 45px; top: 0px;" spellcheck="false" wrap="off"\>\</textarea\>

1

2

3

4

5

6

$hiddencolumnheaders = foreach($column in $worksheet.UsedRange.Columns){

if($column.hidden -eq $true){

$column.value2 | select -first 1

}

}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

\<p style="padding-left: 40px;"\>[/quote] Oh interesting approach! Is value2 a method you found on MS docs?\</p\> Also, how can I print thr $column name?

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [April 30, 2020, 1:02am UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/10 "2020-04-30T01:02:47Z")

</div>

No I found it from just using get-member on the worksheet and sub properties/methods

```
$worksheet | gm

$worksheet.someproperty | gm
```

I’m not sure in the context you’re in how to print the name.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [April 30, 2020, 1:13am UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/11 "2020-04-30T01:13:42Z")

</div>

How about this?

```
foreach ($worksheet in ($workbook.Worksheets | Where { <# $_.Visible -eq -1 #> $_.Name -ne 'Security' -and $_.Name -ne 'Notes' })) {
    if($ExcludeHiddenColumns) {
        $ColumnsCount = $worksheet.UsedRange.Columns.Count
        for ($i=1; $i -le $ColumnsCount; $i++)
        {
            $column = $worksheet.Columns.Item($i).EntireColumn #$worksheet.sheets.columns.entirecolumn.hidden=$true
            if ($column.hidden -eq $true)
            {
                $columnname = $column.cells.item(1,$i).value2
                "Column {0} was deleted!" -f $columnname
                $column.Delete()
                #$i = $i - 1
            }
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![cataster](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@cataster](https://forums.powershell.org/u/cataster)\
**Post date:** [April 30, 2020, 10:21am UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/12 "2020-04-30T10:21:55Z")

</div>

[quote quote=224061][/quote]  
this works! but you should edit it to (1,$i) because it threw error for the period (1.$i)

_ **Unexpected token ‘1.$i’ in expression or statement.** _

btw, i tried the other code

`$hiddencolumnheaders = foreach($column in $worksheet.UsedRange.Columns){`  
`if($column.hidden -eq $true){`  
`$column.value2 | select -first 1`  
`}`  
`}`

and it didnt do anything…nothing was selected and it still exported the hidden columns to the csv file generated…only the code i posted seems to work/output something…

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [April 30, 2020, 1:48pm UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/13 "2020-04-30T13:48:09Z")

</div>

Yes, that should be a comma, good catch! In the last code you said you tested, all it should’ve done was capture the column names of hidden columns in the $hiddencolumns variable. It doesn’t output anything.

---

<div class="post-metadata">

**Author:** ![cataster](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@cataster](https://forums.powershell.org/u/cataster)\
**Post date:** [May 26, 2020, 1:15pm UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/14 "2020-05-26T13:15:34Z")

</div>

[quote quote=224298]Yes, that should be a comma, good catch! In the last code you said you tested, all it should’ve done was capture the column names of hidden columns in the $hiddencolumns variable. It doesn’t output anything.

[/quote]  
hey Doug

its been a while and thanks for your past help 🙂

i have a new thread related to this, would you kindly check it out? Since youve helped me out with this program, i figured i’d ping you about it since this thread is related to it.

[https://powershell.org/forums/topic/how-to-skip-export-of-empty-rows/](https://powershell.org/forums/topic/how-to-skip-export-of-empty-rows/)

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [May 26, 2020, 1:35pm UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/15 "2020-05-26T13:35:40Z")

</div>

You may have to add me to payroll soon.

---

<div class="post-metadata">

**Author:** ![cataster](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@cataster](https://forums.powershell.org/u/cataster)\
**Post date:** [May 26, 2020, 1:41pm UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/16 "2020-05-26T13:41:25Z")

</div>

[quote quote=231151]You may have to add me to payroll soon.

[/quote]  
hahaha yessir

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/how-to-ignore-hidden-excel-columns-and-export-only-visible-ones-to-csv/14291/17 "2024-05-16T20:29:27Z")

</div>

Closed on May 16, 2024, 8:29pm UTC


